Python Data Types

Author
By Darío Rivera
Posted On in Python

In Python 3, all data types are classes. This is very reminiscent of other languages ​​like Dart or even JavaScript itself. In this post, we will see the data types that come incorporated with Python 3 and some examples of them.

In the following table, the first column lists the class that represents the data type, followed by its description and the values it can take.

TypeDescriptionValue
bool Boolean valuesFalse True
NoneTypeAbsence of ValueNone
int Integer Numbers a number
float Floating point numbers decimal numbers
str Character strings a string

In the following examples, we use type to see the specific data type in Python.

print(type(True))          # <class 'bool'>
print(type(None))          # <class 'NoneType'>
print(type(7))             # <class 'int'>
print(type(7.9))           # <class 'float'>
print(type('hello world')) # <class 'str'>

Bool Type

The bool type represents truth values in Python. This data type is fundamental for decision making, conditions, and control flow. The following block will print "Access granted".

is_logged_in = True
if is_logged_in:
    print("Access granted")

Something that surprises developers, is that bool is a subclass of int. Let's take a look at the following demonstration:

print(True == 1)   # True
print(False == 0)  # True

That being said, the following example seems more logical. The result of the operation is shown as a comment.

True + True  # 2

None Type

None is a special value in Python that represents the absence of a value. It is the only instance of the type NoneType. This special value is automatically returned when a function doesn't return anything explicitly.

def log_message(message):
    print(message)

result = log_message("Hello")
print(result)  # None

Although it could be explicitly returned in several scenarios. For instance, it is usually returned when something has not been found.

def find_user(users, name):
    for user in users:
        if user == name:
            return user
    return None

When checking for None, do not use ==, instead always use is. This is because None is a singleton, which means there is only one instance of it in Python.

if value is None:
    ...

Int Type

The int type represents whole numbers (positive, negative, or zero).

age = 30
temperature = -5
count = 0

Float Type

The float type represents numbers with decimal points.

price = 19.99
temperature = -3.5
pi = 3.14159

Unlike int, floats are used when you need fractional values.

String Type

The string data type can be defined in Python using single or double quotes.

str1 = 'hello'
str2 = "world"

A multiline string can be printed using the escape character or triple quotes. The following two examples are equivalent.

x = 'hello\n' \
    'world'
x = '''hello
world'''

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.