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.
Table of Contents
Basic and advanced Python data types
We distinguish between basic types and 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:
- Integers
- Floating point numbers
- Complex numbers
- Booleans
- Strings
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. For example, 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. Examples of immutable data types in Python are:
Python data types that are mutable are:
Why and when are data types mutable?
We havenβt explored 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 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.
Letβs look at an example where we assign the string βhelloβ to a variable called mystring
, and then change it:
>>> mystring = 'hello' >>> mystring = 'world'
What we are doing is reassigning a new value to a variable. Weβre not changing the string βhelloβ itself.
Thereβs another way to explain this. A variable points to a spot in your computerβs memory. That is what we call a pointer. In the first instance, mystring
points to a spot in memory where we stored the string βhello.β After changing mystring
to βworld,β it points to another spot in memory where we store the word βworld.β We didnβt change the string βhello.β
We can prove this by doing the following:
>>> mystring = 'hello' >>> mycopy = mystring >>> mystring = 'world' >>> print(mycopy) 'hello'
We created a second variable pointing to the string βhelloβ. When we changed mystring
to point to a different string, we could still reference the previous string because mycopy
also points to the βhelloβ stringβs location in memory.
This is different from a list, for example. If the variable mylist
points to a list structure in memory, and when we change that list, it still points to the same list structure. All we do is change the list structure itself (its content). Python doesnβt replace the list but modifies it instead.
How do I 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!