Numbers in Python

This tutorial will teach you about Python numbers and how to use them in your programs.

Python various various data types like integers, floats, and complex numbers. This tutorial only covers integers and floats.

Integers

Integers are numbers with the type int, such as -1, 0, 1, 2, 3,…

Math operators such as +, -, *, and / can be used to create expressions that include integers. As an example:

>>> 20 + 10
30
>>> 20 - 10
10
>>> 20 * 10
200
>>> 20 / 10
2.0

Exponents are calculated using two multiplication symbols (**). As an example:

>>> 3**3
27

The parentheses are used to change the order of operations (). As an example:

>>> 20 / (10 + 10)
1.0

Floats

A floating-point number is any number with a decimal point. The term float refers to the fact that the decimal point can appear at any point in a number.

In general, floats can be used in the same way that integers are. As an example:

>>> 0.5 + 0.5
1.0
>>> 0.5 - 0.5
0.0
>>> 0.5 / 0.5
1.0
>>> 0.5 * 0.5
0.25

A float is always returned when two integers are divided:

>>> 20 / 10
2.0

Any arithmetic operation that combines an integer and a float yields a float:

>>> 1 + 2.0
3.0

Python will strive to express the result as precisely as possible due to the internal representation of floats. However, you can get a different result than you expected. For instance:

>>> 0.1 + 0.2
0.30000000000000004

When working with floats, keep this in mind. Later tutorials will teach you how to deal with scenarios like these.

Numbers underscoring

It’s difficult to read a large number. As an illustration:

count = 10000000000

You can use underscores to group digits to make large numbers more readable, like this:

count = 10_000_000_000

Python just discards the underscores when storing these values. When numbers containing underscores are displayed on screen, this is the case:

count = 10_000_000_000
print(count)

Output

10000000000

Both integers and floats can be represented using underscores.