August 8, 2022

Data Types in Python (A Simple Beginner’s Guide)

Python implements built-in data types, so programmers need to know their options — and when to use each one

Python is an object-oriented language, and everything in it is an object. Each object is a specific type, and a class defines each type — this is the definition of a data type in Python. When instantiating an object belonging to a class, we're creating a new variable — the class is the type of this variable.

Python has multiple different data types. So, when programming in Python, it's essential to be aware of the available data types and the ones you're using, since every type of object represents different things and has its own methods, attributes, and functionalities.

In this article, we'll go over the most commonly used built-in data types in Python, as well as some of their methods and use cases.

Data Types

Python is a dynamically typed language. This means that the user doesn't need to specify the variable type when creating it because Python will figure this out itself. 

This also means that it's possible to convert a variable to a different type than its original one, since, of course, the conversion is possible (as we'll see later in this article).

Python Integers

When dealing with Python numbers, you have two main options: integers and floats.

As the name suggests, the integer type is used to work with integer numbers, such as age, years, counts, and more.

>>> age = 37
>>> print(age)
37
>>> print(type(age))
37
<class 'int'>

Notice how the type() function is used to show the data type. Also, the output of such function explicitly shows that the variable age is an instance the int class.

Python Floats

The float type is used for decimal numbers, such as weight, height, and averages.

>>> average_age = 25.5
>>> print(average_age)
25.5
>>> print(type(average_age))
<class 'float'>

When working with floats, the round() function is very useful for rounding the decimal cases:

>>> average_age = 25.539273
>>> print(round(average_age, ndigits=1))
25.5

The n_digits parameter sets the number of decimals.

You can easily convert a float to an int and vice-versa, but we can see the limitations. If you convert a float to an integer, for instance, the resulting number will only be the integer part:

>>> print(int(average_age))
25

When the conversion goes in the other direction, there's no change in the value of the variable, only in the type:

>>> print(float(age))
37.0

Python Strings

The string type is used for chains of characters in general (but more commonly for text). The characters need to be placed between quotes. In Python, you can use either single or double quotes as long as you start and finish the sequence with the same. Also, it's a good practice to keep the choice of quotes consistent throughout the code.

>>> name = 'Matt'
>>> email = '[email protected]'
>>> website = 'www.matt.com'
>>> print(name)
Matt
>>> print(email)
[email protected]
>>> print(website)
www.matt.com
>>> print(type(name))
<class 'str'>

The str class in Python comes with multiple built-in methods to perform operations with strings.

The replace() method, for instance, replaces any characters or sequences of characters inside a string for another one:

>>> print(email.replace('dataquest.io', 'gmail.com'))
[email protected]

The split() method will split the string on a given character:

>>> print(website.split('.'))
['www', 'matt', 'com']

Here are some other commonly used methods:

upper() Transforms the string to uppercase
lower() Transforms the string to lowercase
title() Transforms the first letter of each word to uppercase
find() Looks for a specified character or sequence of characters in the string
startswith() Checks if the string starts with a particular character or sequence of characters
strip() Removes leading and trailing characters as specified

Python Booleans

The Boolean data type is a variable that represents one of two values: true or false. In Python, they are represented by the keywords True and False. They help keep track of conditions or compare values.

When you compare two values, the result of the comparison is always a Boolean value:

>>> print(name == email)
False

The output shows that the values stored in the name and email variables aren't the same. However, if we use some string methods, we can change this outcome:

>>> print(name.lower() == email.split('@')[0])
True

Booleans can also set conditions for the code. For instance, we can assign the result of the above operation to a variable and use it in an if statement:

>>> my_boolean = name.lower() == email.split('@')[0]
>>> if my_boolean == True:
...     print("My boolean is true")
My boolean is true
>>> print(type(my_boolean))
<class 'bool'>

Or you can use the shorter version:

>>> if my_boolean:
...     print('My boolean is true.')
My Boolean is true.

Booleans can also function as operators. The and and or operators in Python are actually Boolean operators. They take two Boolean values and return a new Boolean value based on the values they were given.

Python NoneType

In Python, the None keyword is an object of the NoneType class, which represents the absence of value (and it is Python's Null from other programming languages). It's not zero, it's not false — it's no value at all.

For example, when a parameter of a function doesn't have a return value, then its value is None. Also, when you assign the result of a function that doesn't return anything to a variable, this value in this variable will also be None, as in the example below:

>>> def example(text):
...     print(text)
...
>>> variable = example('test')
>>> print(variable)
test
None

And if you check the type of variable . . .

>>> print(type(variable))
<class 'NoneType'>

Although we can assign None to a variable, we cannot create new instances of the NoneType class. This is fine:

>>> a = None
>>> print(a)
None

But this raises an error:

a = NoneType()
NameError: name 'NoneType' is not defined

Python Lists

In Python, lists are built-in objects that store a collection of items. They are similar to arrays in other programming languages, but they aren't as restricted in what they can store. Lists can contain any combination of data types, including other lists.

For instance, a list can contain integers, floats, strings, Booleans, lists containing other combinations of data types, and so on.

Here's an example:

>>> matts_list = ['Matt', '[email protected]', 'www.matt.com', 37, True]
>>> print(matts_list)
['Matt', '[email protected]', 'www.matt.com, 37, True]

You can create an already-populated list like the one above, or you can create an empty list and populate it. To add values to a list, the append() method is useful. The code below produces the same output as the code above:

>>> matts_list = []
>>> matts_list.append('Matt')
>>> matts_list.append('[email protected]')
>>> matts_list.append('www.matt.com')
>>> matts_list.append(37)
>>> matts_list.append(True)
>>> print(matts_list)
['Matt', '[email protected]', 'www.matt.com', 37, True]

To access an element, you can use brackets and inform the element's index. As with everything in Python, the first element indexes as zero. For example, this is the code to access Matt's email:

>>> matts_email = matts_list[1]
>>> print(matts_email)
[email protected]

Accessing multiple sequenced elements is called slicing, and the syntax is my_list[index_of_first_element: index_of_last_element + 1]. So, to access the email and website at the same time, this is what we need to do:

>>> email_website = matts_list[1:3]
>>> print(email_website)
['[email protected]', 'www.matt.com']

Notice that the output of slicing is also a list.

These are other useful methods:

pop() Removes item at a given index
lower() Removes a given element
clear() Removes all elements from a list
sort() Sort the list alphabetically

Python Tuples

In Python, a tuple is a data structure very similar to a list but with a few important differences. Tuples are immutable, meaning we cannot change them after we create them, which can be useful when you need to store data that shouldn't be modified. 

Tuples are also typically used for storing related pieces of information, such as the coordinates of a point or the dimensions of an object. Unlike lists, tuples are created by enclosing values in parentheses. Here's an example:

>>> matts_tuple = ('Matt', '[email protected]', 'www.matt.com', 37, True)
>>> print(matts_tuple)
('Matt', '[email protected]', 'www.matt.com', 37, True)

Just like lists, tuples can store any type of data, but since they cannot be modified, there are only a couple of methods:

  1. count(): counts the number of a given value
  2. index(): finds an element by its index

You can easily store a list inside a tuple or a tuple inside a list:

>>> matts_list = ['Matt', '[email protected]', 'www.matt.com', 37, True, (39.759991, -86.163712)]
>>> print(matts_list)
['Matt', '[email protected]', 'www.matt.com', 37, True, (39.759991, -86.163712)]

Python Dictionaries

A dictionary is a Python data structure that stores data in key-value pairs. This means that each element is associated with a key, and you can use the key to retrieve the corresponding value. 

Like lists, dictionaries are mutable, which means you can add, remove, and update their entries. They are commonly used to store data in a structured way. For example, you could use a dictionary to store information about a person, like their name, age, address, phone number, and more.

Take a look at the matts_list above: you don't know right away what everything means. You know it's information about a person — you can see a first name, email, and website — but it's difficult to tell what the other entries mean, and you can only access them by the index.

But in a dictionary, you can store each variable paired with a key that enables you to know what that variable is and retrieve it by its name. Here's an example of a dictionary:

>>> matts_dict = {'first_name': 'Matt', 'email':    
...              '[email protected]','website': 'www.matt.com', 'age': 37, 
...              'married': True,'house_coordinates': (39.759991, -86.163712)}
>>> print(matts_dict)
{'first_name': 'Matt', 'email': '[email protected]', 'website': 'www.matt.com', 'age': 37, 'married': True, 'house_coordinates': (39.759991, -86.163712)}

Okay, now you know that 37 is, in fact, Matt's age, that the Boolean variable indicates that Matt is married, and that those numbers in the tuple are the coordinates of his house.

If you want to access any of the data, you can call it by the name of its key:

>>> matts_age = matts_dict['age']
>>> print(matts_age)
37

The process of changing the value of a variable is similar:

>>> matts_dict['first_name'] = 'Matthew'
>>> print(matts_dict['first_name'])
Matthew

And it's possible to add more variables too:

>>> matts_dict['nickname'] = 'Matt'
>>> print(matts_dict)
{'first_name': 'Matt', 'email': '[email protected]', 'website': 'www.matt.com', 'age': 37, 'married': True, 'house_coordinates': (39.759991, -86.163712), 'nickname': 'Matt'}

Dictionaries have several built-in methods, such as keys() and values(), which allow you to access the data in the dictionary like this:

>>> print(matts_dict.keys())
dict_keys(['first_name', 'email', 'website', 'age', 'married', 'house_coordinates', 'nickname'])
>>> print(matts_dict.values())
dict_values(['Matthew', '[email protected]', 'www.matt.com', 37, True, (39.759991, -86.163712), 'Matt'])

Python Sets

In Python, a set is a collection that, unlike lists and tuples, is unordered and unindexed, which means it isn't possible to access any item by index. We use sets for storing unique values since they cannot have duplicates. 

We can use sets to perform mathematical set operations like union, intersection, and difference. We write them in curly brackets:

>>> matts_set = {'Matt', '[email protected]', 'www.matt.com', 37, True, (39.759991, -86.163712)}
>>> print(matts_set)
{True, 37, 'www.matt.com', '[email protected]', 'Matt', (39.759991, -86.163712)}

Notice that the order of the set in the output is different from the order in the code. That's because, as mentioned, sets don't store any order at all.

We also use sets to remove duplicates from lists. Once you transform a list with duplicates into a set, it'll lose the duplicates, and then you can transform it back to a list and have a list with no duplicates:

>>> duplicates_list = ['Matt', 'Matt', 37]
>>> set_from_list = set(duplicates_list)
>>> new_list = list(set_from_list)
>>> print(new_list)
[37, 'Matt']

It will, of course, be unordered.

Conclusion

In this article, we covered the main characteristics and methods of the most important and most commonly used Python data types:

  • Int
  • Float
  • Str
  • Bool
  • List
  • Tuple
  • Dict
  • Set
Otávio Simões Silveira

About the author

Otávio Simões Silveira

Otávio is an economist and data scientist from Brazil. In his free time, he writes about Python and Data Science on the internet. You can find him at LinkedIn.

Learn data skills for free

Headshot Headshot

Join 1M+ learners

Try free courses