Numbers

Congratulations, you are already probably familiar with numbers!

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)
<class 'int'>
2
x = 4.5
print(type(x))
print(x)
<class 'float'>
4.5
x = 2 / 3
print(type(x))
print(x)
<class 'float'>
0.6666666666666666
x = 2.0
print(type(x))
print(x)
<class 'float'>
2.0

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)
<class 'int'>
120000
x = 120_000.0
print(type(x))
print(x)
<class 'float'>
120000.0

Operations

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)
12
-8
20
0.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))
7.0
6.7
6.68
6.676
6.6756
6.67556