4 / 14
Guide Book · Chapter 4

Numbers & math operators

int, float, complex and the arithmetic operators, including the corners that surprise people.

The number types

TypeExampleNotes
int42, -7, 1_000_000, 0xff, 0b1010unlimited size
float3.14, 1e-3, float("inf")64-bit IEEE 754 double
complex2+3jreal + imaginary parts
boolTrue, Falsea subclass of int (1 and 0)
python
print(2 ** 200)                  # ints never overflow
print(0xff, 0b1010, 0o17, 1_000_000)
print(type(3.0), type(3), type(2+3j))
print(True + True)               # bool is an int: 2

Arithmetic operators

OperatorMeaning7 ? 2-7 ? 2
+ - *add, subtract, multiply9, 5, 14-5, -9, -14
/true division, always float3.5-3.5
//floor division, rounds toward −∞3-4
%remainder, takes the divisor's sign11
**power4949
python
print(7 / 2, 7 // 2, 7 % 2)
print(-7 // 2, -7 % 2)            # -4 and 1, not -3 and -1!
print(divmod(17, 5))              # (quotient, remainder) in one go
print(2 ** -1, (-8) ** (1/3))     # negative/fractional powers give floats (or complex)

Float precision

Floats are stored in binary, so most decimals are approximations:

python
print(0.1 + 0.2)                  # 0.30000000000000004
print(0.1 + 0.2 == 0.3)           # False!

import math
print(math.isclose(0.1 + 0.2, 0.3))   # True: compare floats with a tolerance
print(round(2.675, 2))            # 2.67, because 2.675 is really 2.67499999…
python
print((0.1).as_integer_ratio())
print((0.5).as_integer_ratio())   # 0.5 is exact in binary
print(float.hex(0.1))

For money, use decimal.Decimal or count cents as ints:

python
from decimal import Decimal

print(Decimal("0.1") + Decimal("0.2"))       # 0.3 exactly
price_cents = 1999
print(f"${price_cents / 100:.2f}")
python
import sys

for n in (1, 2 ** 30, 2 ** 60, 2 ** 1000):
    print(len(str(n)), "digits →", sys.getsizeof(n), "bytes")

Useful built-ins & math

python
import math

print(abs(-3), round(3.14159, 2), round(2.5), round(3.5))   # banker's rounding: 2, 4
print(max(3, 9, 1), min([4, 2]), sum([1, 2, 3]))
print(int("42"), int("ff", 16), float("1e3"), int(3.99))     # int() truncates
print(math.floor(-2.5), math.ceil(-2.5), math.trunc(-2.5))
print(math.sqrt(16), math.pi, math.gcd(12, 18), math.factorial(5))

⚠️ Gotcha: round() uses banker's rounding: halves round to the nearest even number, so round(2.5) is 2. It avoids bias when you round many values.

🧭 Scenario: Splitting a bill: 100 among 3 people. 100 / 3 gives 33.333…, but money needs whole cents. Use divmod on cents: everyone pays 3333 and the first person pays the remaining 1 cent.

python
total_cents, people = 10000, 3
share, extra = divmod(total_cents, people)
payments = [share + (1 if i < extra else 0) for i in range(people)]
print(payments, sum(payments) == total_cents)
main.py
Loading editor…
No output yet
Press Run or ⌘↵ to compile and run your code.

Search

Jump to a lesson or a past run