9  Numbers

Congratulations, you are already probably familiar with numbers!

9.1 Characteristics

In Python, there are two kinds of numbers: integers and floats. Integers are whole numbers without a decimal component, while floats have decimal component (even if it is X.0):

x = 2
print(type(x))
print(x)
x = 4.5
print(type(x))
print(x)
x = 2 / 3
print(type(x))
print(x)
x = 2.0
print(type(x))
print(x)

When dealing with large numbers, it may be helpful to visually separate using underscores. These variants are treated as if the underscores were not there:

x = 120_000
print(type(x))
print(x)
x = 120_000.0
print(type(x))
print(x)

9.2 Operations

9.2.1 Arithmetic Operations

It is no surprise, we can use Python as a glorified calculator, to perform arithmetic operations:

print(2 + 10)
print(2 - 10)
print(2 * 10)
print(2 / 10)

9.2.2 Rounding

It is possible to round numbers using the round function:

print(round(6.675555, 0))
print(round(6.675555, 1))
print(round(6.675555, 2))
print(round(6.675555, 3))
print(round(6.675555, 4))
print(round(6.675555, 5))