Python Data Types: Learn Them All, With Example Code

In this section, we take a close look at the most important Python data types. Python provides us with several native data types to store and process data. These are essential building blocks that you need to know well. When thinking about a problem, part of the solution is often choosing the right data type. Knowing what each data type is capable of, will make it much easier to pick the right one.

Basic and advanced Python data types

We make a distinction between basic types and the more advanced data structures. The basic data types in Python store a single value, like a number or a piece of text. The basic data types in Python are:

Next, we have the more advanced Python data types. They can store many items, like lists of items or key-value pairs:

These types all have distinctive features that set them apart from the others. E.g., ranges are efficiently calculated on the fly, tuples can not be modified (while lists can), and sets allow you to do mathematical set calculations.

Mutability in Python

Python data types can be categorized into two classes: mutable and immutable. Or, more accurately: hashable and unhashable. An object is mutable if we can change the data it holds, and it’s immutable if we can’t change it. Examples of immutable data types in Python are:

Python data types that are mutable are:

Why are these data types mutable? We didn’t dive into all these types yet, but let’s take a list as an example. Without knowing the exact details, I can tell you that you can add more items to a list, remove items, and replace them. These are not surprising features for a list, right? So a list can be changed, hence it is mutable.

However, an integer is simply a number, like 2, 3, and 4. You can’t change a number; it is what it is. I can almost hear you thinking now. “But I can change a Python variable, even if it’s an integer!” You’re right, but that’s something different though.

Let’s look at an example where we assign the integer 2 to a variable called myint, and then change it:

>>> myint = 2
>>> myint = 3

What we are doing, is reassigning a new value to a variable. We’re not changing the data, the number 2, itself.

There’s another way to explain this. Maybe you are familiar with pointers? A variable points to a spot in your computer’s memory. That is what we call a pointer. In the first instance, myint points to a spot in memory where we stored the number 2. After changing myint to 3, it points to another spot in memory where we store the number 3. They are different numbers, in different locations in your computer’s memory. All we do is point myint to another location.

This is different from a list, for example. If the variable mylist points to a list structure in memory, and we change that list, it still points to the same list structure. Python doesn’t replace the list but modifies it instead.

How to check the Python data type?

There’s a built-in function called type which you can use to check data types in Python. Let’s look at some examples of type at work:

>>> type(3)
<class 'int'>
>>> type('hello')
<class 'str'>
>>> type([1,2,3])
<class 'list'>

If you are experimenting in the REPL, type is a valuable function that can give you more insight into what’s happening under the hood!

About Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments