Python Data Types

To store data in Python, you need some variable, and every variable has its type depending on the value of the data stored.

Python has dynamic typing, which means you don’t have to explicitly declare the type of your variable, but, if you want, you can.

This post is supposed to be a summary of all the types in Python, it will link to other posts with more detailed explanations on each type.

One more detail, in Python, everything is an object.

A variable (object) is just an instance of its type (class).

Determining the Type

First of all, let’s learn how to determine the data type.

Just use the type() function passing the variable of choice as an argument, like the example below.

print(type(my_variable))

String

The text type is one of the most commons types out there and is often called string or, in Python, just str.

my_city = "New York"
print(type(my_city))
#<class 'str'>

#Single quotes have exactly the same use as double quotes
my_city = 'New York'
print(type(my_city))
#<class 'str'>

#Setting the variable type explicitly
my_city = str("New York")
print(type(my_city))
#<class 'str'>

Notice the output of the type(). It shows the type as an object of the class str.

Numbers

There are three types of numeric types: int, float and complex.

Integer

my_int = 32
print(type(my_int))
#<class 'int'>
my_int = int(32)
print(type(my_int))
#<class 'int'>

Float

my_float = 32.85
print(type(my_float))
#<class 'float'>
my_float = float(32.85)
print(type(my_float))
#<class 'float'>

Complex

my_complex_number = 32+4j
print(type(my_complex_number))
#<class 'complex'>
my_complex_number = complex(32+4j)
print(type(my_complex_number))
#<class 'complex'>

Sequences

There are three types of sequence: list, tuple, range.

List

A list has its items ordered and you can add the same item as many times as you want. An important detail is that lists are mutable.

my_list = ["bmw", "ferrari", "maclaren"]
print(type(my_list))
#<class 'list'>
my_list = list(("bmw", "ferrari", "maclaren"))
print(type(my_list))
#<class 'list'>

Tuple

A tuple is just like the list: ordered, allows repetition of items.

There is just one exception: a tuple is immutable. I will explore the concept of immutable objects in a dedicated post.

my_tuple = ("bmw", "ferrari", "maclaren")
print(type(my_tuple))
#<class 'tuple'>
my_tuple = tuple(("bmw", "ferrari", "maclaren"))
print(type(my_tuple))
#<class 'tuple'>

Range

It works as a helper to generate a sequence of integers starting from the number in the first argument and the stopping in the number in the second argument.

If you set one argument, the first argument has its default in 0.

You can also set a third argument determining the step.

my_range = range(32)
print(type(my_range))
#<class 'range'>

Dictionary

The dictionary doesn’t guarantee the order of the elements and is mutable.

One important characteristic in dictionaries is that you can set your own access keys for each element.

my_dict = {"country" : "France", "worldcups" : 2}
print(type(my_dict))
#<class 'dict'>
my_dict = dict(country="France", worldcups=2)
print(type(my_dict))
#<class 'dict'>

Posts on dictionaries:

Sets

There are two types of set: set, frozenset

Sets don’t guarantee the order of the items and are not indexed.

A key point when using sets: they don’t allow repetitions of an item.

The difference between a Set and a FrozenSet is the former is mutable and the latter is immutable.

Set

my_set = {"bmw", "ferrari", "maclaren"}
print(type(my_set))
#<class 'set'>

my_set = set(("bmw", "ferrari", "maclaren"))
print(type(my_set))
#<class 'set'>

FrozenSet

my_frozenset = frozenset({"bmw", "ferrari", "maclaren"})
print(type(my_frozenset))
#<class 'set'>

Boolean

The boolean type is one of the most basic types of programming.

A boolean typed variable can only represent either True or False.

my_bool = True
print(type(my_bool))
#<class 'bool'>

my_bool = bool(1024)
print(type(my_bool))
#<class 'bool'>

To know more about booleans, read my other post Booleans in Python.

Binary

There are three binary types: bytes, bytearray, memoryview.

As the name implies, these types allow you to handle binary data directly, without conversions.

Bytes are immutable while Bytearrays are mutable and they both deal with sequences of single bytes.

Memoryview lets you access data in an object without having to copy it, which is handy if the memory buffer you are targeting is rather large and you need some very good level of optimization.

Bytes

my_bytes = b"New York"
print(type(my_bytes))
#<class 'bytes'>

my_bytes = bytes(32)
print(type(my_bytes))
#<class 'bytes'>

Bytearray

my_bytearray = bytearray(32)
print(type(my_bytearray))
#<class 'bytearray'>

Memoryview

my_memoryview = memoryview(bytes(32))
print(type(my_memoryview))
#<class 'memoryview'>