Skip to content

Integers

Arithmetic operations are expressed in Python as expected:

Example
4+2
1-2
1*3
4/2
2**3 # 2 hoch 3

Please note that the result of an integer division is a decimal number. Integer division is rounded off with //:

Example
2//2 # Result: 1
3//2 # Result: 1

Python can process integers of any size:

Example
10**100 # 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

The modulo operator calculates the remainder after integer division:

Example
10 % 3 # 1

Exponents can be both integers and decimal numbers. The return value is always an integer if the base and exponent are both integers and the result can be represented as an integer.

Example
2**3 # 8
2**3.0 # 8.0
2**-3 # 0.125

Conversion

In general, various data types can be converted to integers:

Example
int("2") # 2
int(1.2) # 1
int(1.9) # 1

However, the mixture of the two examples is not immediately successful:

Example
int("1.9") # fails
int(float("1.9")) # works