Data types · 3 / 29 Theory

Basic data types

These are the basic types that represent simple values. Unlike other languages, in Python they are objects like any other, and you do not choose a size or a precision when you use them.

Integers (int)

Whole numbers, positive or negative. Their size is only limited by the available memory:

quantity = 7
debt = -250
big = 2 ** 100
print(big)      # 1267650600228229401496703205376

Operators: +, -, *, / (always returns a float), // (integer division), % (remainder) and ** (power).

print(7 / 2)    # 3.5
print(7 // 2)   # 3
print(7 % 2)    # 1

Floating point numbers (float)

Numbers with decimals. They are stored in binary, so some results have small rounding errors:

price = 19.99
print(0.1 + 0.2)           # 0.30000000000000004
print(round(0.1 + 0.2, 2)) # 0.3

Booleans (bool)

Only two values: True and False. They are the result of comparisons and are used in conditions:

age = 20
is_adult = age >= 18
print(is_adult)            # True
print(is_adult and age < 65)   # True

Logical operators are and, or and not.

Strings (str)

Text between single or double quotes. Strings are immutable sequences of characters:

word = "Python"
print(word[0])         # P
print(word[-1])        # n
print(word[1:4])       # yth
print(len(word))       # 6
print(word + "!")      # Python!
print(word * 2)        # PythonPython

Formatted strings (f-strings) insert values inside the text:

name = "Ana"
age = 25
print(f"{name} is {age} years old")

None

None represents the absence of a value. It is the only value of the type NoneType:

result = None
print(result is None)  # True

There is no distinction between "primitive" types and objects, and no separate character type: a character is simply a string of length 1.