1. int

1.1. Defining int

  • Python 3 dynamically extends int, when it’s too big

  • In Python 3 there is not maximal int value

  • You can use _ for easier read especially with big numbers

value = 30              # 30
value = -30             # -30
million = 1000000       # 1000000
million = 1_000_000     # 1000000

1.2. Converting to int

  • Also known as “type casting”

  • int() converts argument to int

int(10)                 # 10
int(10.0)               # 10
int(10.9)               # 10
int(1.23)               # 1
int(-1.23)              # -1
int('10')               # 10
int('10.5')             # ValueError: invalid literal for int() with base 10: ' 10.5'

1.3. Numerical Operators

1.3.1. Add and subtract

Table 1.1. Add and subtract

Operand

Description

-x

x negation

+x

x

x + y

Sum x and y

x - y

Subtract x and y

1.3.2. Multiplication

Table 1.2. Numerical types operators

Operand

Description

x * y

Multiply x and y

x ** y

x to the power of y

1.3.3. Division

Table 1.3. Numerical types operators

Operand

Description

x / y

Divide x and y

x // y

Quotient of division x by y

x % y

Modulo. Reminder of division x by y

1.3.4. Incremental assignments

  • Add y to x and assign result to x as new value

Table 1.4. Incremental assignments

Operand

Description

x += y

Add and assign

x -= y

Subtract and assign

x *= y

Multiply and assign

x /= y

Divide and assign

1.4. Numeric Functions

1.4.1. Absolute value

abs(1)          # 1
abs(-1)         # 1

1.4.2. Number to the n-th power

pow(2, 2)       # 4
pow(3, 4)       # 81
pow(-1, 2)      # 1
2 ** 2          # 4
3 ** 4          # 81
-1 ** 2         # 1