In this article, you are going to see a brief overview of different Python data types. Data types in a programming language refer to the type of data that the language can work with. While developing an application, you need to work with data of different types, for example, numbers, text, booleans, etc. Python programming language also supports a variety of data types. Some of the basic python data types are enlisted below:

  1. Numeric Types
  2. Strings
  3. Booleans
  4. Lists
  5. Tuples
  6. Dictionaries

It is important to mention that the Python data types can be used as constants or variables. Python is not a strongly typed language which means that you do not need to specify the data type with the variable name.

Let’s briefly take a look at various Python data types with examples.

Numeric Types

Numeric types in Python, as the name suggests, store numeric data. The numeric types in Python are further divided into three types:

Integers

The integer type variables are used to store integers of any length. Look at the following example.

age = 10 
print(type(age))

Here we create an integer variable age and assign it the value 10. Next, using the type() method, the type of the age variable is printed. In the output, you should see “<class ‘int’>”

Output:

<type 'int'>

Floats

Float type variables store numbers with decimals with a precision of up to 14 decimal points. Here is an example:

weight = 170.56
print(type(weight))

Output:

<type 'float'>

Complex Numbers

You can also store complex numbers in Python variables. A complex number consists of magnitude and dimension. The magnitude of a complex number is represented by the numeric part while the dimension is represented by the character. Here is an example:

salary = 10 + 5j
print(type(salary))

Output:

<type 'complex'>

Strings

Python treats strings as sequences of characters. Strings store textual data in Python. Here is an example of how to use strings in Python:

name = "Joseph Juana"
print(type(name))
print(len(name))

In the script above, we create a string variable called name. Next, we print the type of the variable and the length of the string. The len() function counts the number of characters in a string. Here is the output of the above script:

Output:

<type 'str'>
12

Booleans

Boolean variables are used to store boolean values i.e. True or False. Here is an example:

married = True
have_kids = False

print(type(married))
print(type(have_kids))

In the script above we create two boolean variables: married and have_kids. The first variable contains True while the second variable contains False value. Finally, the type of both variables is printed.

Output:

<type 'bool'>
<type 'bool'>

Python data types can also be used to store a collection of items. In this regard, Python offers three data types: lists, tuples, and dictionaries. Let’s review them one by one.

Lists

Python lists store a collection of items of the same or different data types. Python lists are mutable which means that you can update, delete, and add items to a Python list. You can also clear a Python list.

To create lists, you need to pass individual values and separate them by commas inside square brackets, Let’s see a very simple example of a list in Python.

planets = ["Earth", "Mars", "Saturn", "Venus", "Jupiter", "Uranis", "Mercury","Neptune", "Venus"]
type(planets)

Output:

<type 'list'>

To study more about how to use lists in Python, check out our detailed guide on Python lists.

Tuples

Like lists, tuples also store a collection of items. However, unlike lists, tuples are immutable. Hence, you can add, remove or update items in a tuple. To create tuples, you have to pass a collection of items inside round brackets. Here is a very simple example of a tuple in Python.

planets_tuple = ("Earth", "Mars", "Saturn", "Venus", "Jupiter", "Uranis", "Mercury","Neptune", "Venus")
print(type(planets_tuple))

Output:

<type 'tuple'>

As I said earlier, you cannot update a tuple. Let’s try to update the value at the second index of a tuple and see what happens:

planets_tuple[2] = "Sun"

Output:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
 in 
----> 1 planets_tuple[2] = "Sun"

TypeError: 'tuple' object does not support item assignment

The output shows that the tuple assignment is not possible.

Dictionaries

Dictionaries also store a collection of items, but in the form of key-value pairs. Each item consists of a key and a value, which are separated via a colon. The items in a dictionary are separated from each other via a comma. Dictionary items are stored inside curly brackets. Here is an example of a simple dictionary.

patient_dict ={"Name": "John", "Age":10, "Blood_Group":"B+"}
print(type(patient_dict))

The script above creates a dictionary patient_dict. The script then prints the type of the variable patient_dict.

Output:

<type 'dict'>

If you wish to learn more about Python, check out our collection of Python tutorials.