Python Math
Python can do a lot of maths without importing anything, and the standard math module adds the rest of what you'd find on a scientific calculator.
Built-in math functions
| Function | Returns | Example | Result |
|---|---|---|---|
abs(x) |
absolute value | abs(-4.5) |
4.5 |
round(x, n) |
rounded to n decimals |
round(2.675, 1) |
2.7 |
min(...), max(...) |
smallest / largest | max(3, 9, 1) |
9 |
sum(iterable) |
total | sum([1, 2, 3]) |
6 |
pow(x, y) |
x ** y |
pow(2, 10) |
1024 |
divmod(a, b) |
(a // b, a % b) |
divmod(17, 5) |
(3, 2) |
temps = [21.5, 25.0, 19.8, 23.1]
print(min(temps), max(temps))
print(round(sum(temps) / len(temps), 2))
print(abs(-12))
print(pow(2, 10), pow(2, 10, 1000)) # optional third argument: modulus
hours, minutes = divmod(200, 60)
print(f"{hours}h {minutes}m")19.8 25.0
22.35
12
1024 24
3h 20m
The math module
import math
print(math.sqrt(144))
print(math.pi)
print(math.e)
print(math.floor(3.7), math.ceil(3.2))
print(math.trunc(-3.7))
print(math.factorial(6))
print(math.gcd(48, 36), math.lcm(4, 6))
print(math.hypot(3, 4))12.0
3.141592653589793
2.718281828459045
3 4
-3
720
12 12
5.0
math.lcm needs Python 3.9+.
Rounding functions compared
| Function | 3.7 | -3.7 | Rule |
|---|---|---|---|
math.floor |
3 | -4 | down toward −∞ |
math.ceil |
4 | -3 | up toward +∞ |
math.trunc / int() |
3 | -3 | toward zero |
round |
4 | -4 | to nearest (halves to even) |
Powers, roots and logarithms
import math
print(math.pow(2, 0.5))
print(8 ** (1 / 3)) # cube root with the ** operator
print(math.isqrt(50)) # integer square root
print(math.log(math.e)) # natural log
print(math.log10(1000))
print(math.log2(1024))
print(math.log(81, 3)) # log with any base
print(math.exp(1) == math.e)1.4142135623730951
2.0
7
1.0
3.0
10.0
4.0
True
Trigonometry
Trig functions work in radians. Convert with math.radians() and math.degrees():
import math
angle = math.radians(30)
print(round(math.sin(angle), 4))
print(round(math.cos(math.radians(60)), 4))
print(round(math.degrees(math.atan2(1, 1)), 1))
# Distance between two points on a plane
x1, y1, x2, y2 = 1, 1, 4, 5
print(math.dist((x1, y1), (x2, y2)))0.5
0.5
45.0
5.0
Special values and comparisons
import math
print(math.inf > 10 ** 100)
print(math.isinf(float("inf")), math.isnan(float("nan")))
print(math.isclose(0.1 + 0.2, 0.3))
print(math.fsum([0.1, 0.2, 0.3]), 0.1 + 0.2 + 0.3)
print(math.prod([2, 3, 4]))
print(math.comb(5, 2), math.perm(5, 2))True
True True
True
0.6 0.6000000000000001
24
10 20
math.fsum adds floats with extra precision, so rounding errors don't pile up the way they do with plain + (since Python 3.12 the built-in sum() is also compensated for floats); math.comb(n, k) counts ways to choose k items from n and math.perm counts ordered arrangements.
Statistics
For averages and spread, the statistics module is easier than writing the formulas yourself:
import statistics as st
marks = [56, 72, 72, 85, 91, 64]
print(st.mean(marks))
print(st.median(marks))
print(st.mode(marks))
print(round(st.stdev(marks), 2))73.33333333333333
72.0
72
12.96
Tip: For heavy numerical work — big arrays, matrices, vectorised operations — the third-party NumPy library is the standard choice. Install it with pip (see the PIP lesson).
Test yourself
What does math.ceil(4.1) return?
- 4
- 5
- 4.0
- 4.1
In which unit do math.sin and math.cos expect angles?
- Degrees
- Radians
- Gradians
- Either, it detects automatically
What does divmod(23, 4) return?
- (5, 3)
- (6, 1)
- 5.75
- (3, 5)