Python ยท Chapter 7 of 45
Python Numbers
Python has three numeric types: `int` (whole numbers), `float` (decimals) and `complex` (numbers with an imaginary part).
Math operators work as expected: `+`, `-`, `*`, `/`, `//` (floor div), `%` (modulo), `**` (power).
Integer division vs float division
`/` always returns a float. `//` returns the floor (integer) of the division.
Built-in math
`abs()`, `round()`, `pow()`, `min()`, `max()` work on numbers without importing anything. For advanced math use the `math` module.
Example 1 (python)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 8)Output
3.5
3
1
256Division, floor division, modulo and power operators.
Example 2 (python)
x = 3.14159
print(round(x, 2))
print(abs(-7))Output
3.14
7round() and abs() are built-in math helpers.
Key points
- `/` returns float, `//` returns int (floor).
- `**` is power (`2 ** 3` is 8).
- `%` is modulo (remainder).
- Use `math` module for sqrt, sin, log, etc.
๐ก Note: Never compare floats with `==` โ tiny rounding errors can make equal values look different. Use `math.isclose()`.
