Numbers

Arithmetic, / vs //, %, **, round, and the math module.

Integers (int) and floating-point numbers (float) share the usual operators. Division with / always returns a float.

Goal

Use +, -, *, /, //, %, ** and a few math functions.

Arithmetic

print(7 + 3)
print(7 - 3)
print(7 * 3)
print(7 / 3)
print(7 // 3)
print(7 % 3)
print(2 ** 10)

// is floor division. % is remainder.

Mix int and float

print(10 * 0.5)
print(type(10 * 0.5))
print(round(10 / 3, 2))

math

import math

print(math.sqrt(49))
print(math.pi)
print(math.floor(3.9), math.ceil(3.1))
print(math.gcd(24, 18))

Compound assignment

total = 10
total += 5
total *= 2
print(total)

Order

print(2 + 3 * 4)
print((2 + 3) * 4)
Tip

1/0 raises ZeroDivisionError. You will catch errors in a later chapter — for now, read the traceback.