Hey there! Welcome to Hostman! 🎉

Data Types in Python

06.12.2023
Reading time: 7 min
Hostman Team
Technical writer

Python is a fully object-oriented programming language. Hence, all data in it is objects, which can be embedded at the language level or declared manually. 

Objects are instances of a class and the basic building blocks of any program, which are allocated memory to store data in them. So, for example, when we write x = 5 in code, we create an instance of the int class with the value 5 and assign it to the variable x. Now x is an object of the int type.

All objects have unique data types (int, string, list, etc.) and are created using the constructors of their respective classes. Classes, in turn, are mechanisms that define the behavior of objects thanks to unique attributes and methods. 

In this article, we will look at what data types there are in Python and talk about the features of each of them.

Variables

Variables in Python are named memory locations that store a variety of values within a Python program.

Each variable has its own data type, which tells the interpreter what can be stored in the variable and how much memory should be allocated for it.

The declaration of a variable is as follows:

x = 10

First of all, we specify the name of the variable, then we put an equal sign, and after that, we pass the value that will be assigned to the specified variable.

Each variable has its own location in memory. You can determine it using the id() function:

x = 10
print(id(x))

For the variable x from the example above, the location will be like this:

168d493b 3b23 4359 9edc 902cb8ba881a

There is also a type() function in Python, that allows us to find out the data type selected for a particular variable:

x = 10
print(type(x))

As you can see from the image below, an integer data type has been defined for x.

Ec3965d6 B426 48ba B32a E06d6bad2d82

The process of declaring a data type is called typing. It can be static or dynamic. Python uses the latter. That's why when the same variable is used, it can represent objects of completely different data types. For example:

first_example = 'Hello'
print(type(first_example))

first_example = 100
print(type(first_example))

first_example = 3.14
print(type(first_example))

In all cases of calling print, we will see a different result. 

Built-in data types

In this section, we will look at all the basic data types in the Python programming language. They can be divided into two groups: immutable and mutable objects.

You cannot change (mutate) the data in the immutable objects. You can only create a new object with the specified value and the same name. Immutable objects are objects of the numeric, string or tuple type.

Mutable objects, in turn, can be changed. This group includes objects of the list, dictionary or set types.

To check if the object is mutable, let's use the id() function and compare the memory locations before and after changes were made:

first_example = 50
second_example = [50, 100, 150]
print(f'Memory location before changing int object = {first_example} : {id(first_example)}')
print(f'Memory location before changing list object = {second_example} : {id(second_example)}')
first_example += 50
second_example += [200]
print(f'Memory location after changing int object = {first_example} : {id(first_example)}'')
print(f'Memory location after changing list object = {second_example} : {id(second_example)}')

You can see that when performing the addition operation with a variable of int type, a new object is created, the name of which is saved, but the value of the memory location is not. When adding a new value to an object of the list type, the address in memory does not change, which confirms its mutability.

You should take into account the mutability when working with functions. When you call a function with a mutable object as an argument, the function may perform mutations on that object, which will affect the original object. It is generally better to avoid mutating arguments inside your functions.

Numeric and boolean

First of all, let's look at the numeric data type in Python. 

Below is a table containing the name and description of the numeric data type, along with its notation and examples. We also added the boolean data type here.

Data type

Description

Example

Whole numbers (int)

Positive and negative numbers without decimals.

example1 = 5

example2 = -1234

Floating point numbers (float)

Positive and negative numbers with decimals

example3 = 3.14

example4 = -43.4132

Complex numbers (complex)

Numbers in the format: a + bj

where a and b are real numbers and j is an imaginary unit.

Complex numbers are passed using the complex() function.

The syntax for displaying the real part:

variable_name.real

The syntax for displaying the imaginary part:

variable_name.imag

example5 = complex(5,4)

Boolean (bool)

The boolean data type takes only 2 values: True and False

It is often used to implement branching or checkboxes.

example7 = True

example8 = False

Strings

The string data type stores text information or a sequence of bytes. To declare a string, you need to use single, double or triple quotes, as shown in the example below:

example_srt1 = ‘Example string #1’
example_srt2 = “Example string #2”
example_srt3 = '''Example string #3'''

As we said before, strings are immutable objects.

Lists

List in Python is an ordered collection of items. These items can be of different data types. Lists are mutable, meaning you can add, remove, and modify elements in a list.

You can create a list by using square brackets with elements between them, separated by commas, as shown in the example below:

example_list = [1, 5.7, 'hello']

To retrieve elements from a list, use indices. You can retrieve a single element or a slice. Example:

example_list = [1, 5.7, 'hello']
print(f'Third element of the list: {example_list[2]}')
print(f'First and second element of the list: {example_list[0:2]}')

Tuples

Tuples in Python are similar to lists but immutable. Their advantage is that the interpreter works with them much faster. So, if you want your program to run faster and your data to remain immutable, use tuples.

To create a tuple, we need to put comma-separated elements inside parentheses, as shown in the example below:

example_tuple = (1, 5.7, 'hello')

As to retrieving elements, we can do it using indices, just like lists. Just remember that you cannot mutate them.

Dictionaries

Dictionaries are an unordered list of key-value pairs. They provide quick access to data by using the appropriate key.

To declare a dictionary, list the data in a key-value format in curly braces, as shown in the example below:

example_dict = {
    'name':'Peter',
    'age':33,
    'country':'Netherlands'
}

It's important to remember that values can be of any data type, and keys are only immutable.

To get a value, use the corresponding key:

example_dict = {
    'name':'Peter',
    'age':33,
    'country':'Netherlands'
}

print(f'Peter is from: {example_dict["country"]}')
print(f'His age is: {example_dict["age"]}')

Sets

A set is a collection of unique immutable elements.

To declare a set, you must list the elements separated by commas in curly braces, as shown in the example below:

example_set = {1,2,3,4,5}

If we try to pass duplicate elements into a set, the interpreter will automatically remove them. 

example_set = {1,2,3,4,5,1,2,3,4,5}
print(example_set)

As a result, the print(example_set) function will print the following:

F7c75f0d 64a6 4547 8034 23aa9f277a5d

Another feature of sets is that we can perform a number of mathematical operations (union, difference, intersection) on them.

What to remember

  • All objects have their own unique data types (int, string, list, etc.) and are created using the constructors of the corresponding classes.

  • Variables in Python are named memory locations that store values. Each variable has its own data type, which tells the interpreter what can be stored in that variable and how much memory should be allocated to it.

  • You cannot change the state and contents of immutable objects. Instead, you can create a new object with the specified value and the same name. Immutable objects are of the number, string or tuple types.

  • Mutable objects can be changed. This group includes the list, dictionary, and set types.

  • The numeric data type includes integers, floating point numbers, and complex numbers.

  • The boolean data type takes only two values: True or False.

  • The string data type stores text information or a sequence of bytes.

  • Lists are ordered collections of items that can be of different data types.

  • Tuples in Python are the same as lists, but immutable.

  • A dictionary is an unordered list of key-value pairs.

  • A set is a collection of unique and unordered elements of an immutable type.