Python Reference ยท Standard Library: Math, Data and Text
The math module
Reference for Python's math module: square roots, rounding, powers, logarithms, trigonometry, constants, gcd, factorial, combinatorics and float comparison.
The math module collects the mathematical functions and constants you reach for beyond the basic operators: square roots, logarithms, trigonometry, rounding to integers, greatest common divisors, factorials and more. Almost all of them work on real numbers and return a float; the integer functions (gcd, factorial, comb, isqrt) return exact int values of any size.
The module does not handle complex numbers, which have their own module, cmath. Passing a value outside a function's domain raises ValueError, for example math.sqrt(-1).
import math print(math.sqrt(16), math.pi)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Frequently used names
| Name | Description |
|---|---|
sqrt(x), cbrt(x) |
Square root; cube root (3.11+). sqrt of a negative number raises ValueError. |
isqrt(n) |
Integer square root, rounded down, exact for huge integers. |
floor(x), ceil(x), trunc(x) |
Round toward minus infinity, plus infinity or zero. All return int. |
pow(x, y), exp(x), exp2(x) |
x**y always as a float; e**x; 2**x (3.11+). |
log(x[, base]), log2(x), log10(x), log1p(x) |
Natural log by default. log1p(x) is accurate for tiny x. |
sin, cos, tan, asin, acos, atan, atan2(y, x) |
Trigonometry in radians. |
degrees(x), radians(x) |
Convert between radians and degrees. |
hypot(*coords), dist(p, q) |
Euclidean length and distance in any number of dimensions. |
gcd(*ints), lcm(*ints) |
Greatest common divisor and least common multiple (multiple arguments since 3.9). |
factorial(n), comb(n, k), perm(n, k) |
Exact combinatorics for non-negative integers. |
prod(iterable), fsum(iterable), sumprod(p, q) |
Product; accurate float sum; sum of products (3.12+). |
isclose(a, b, rel_tol=1e-09, abs_tol=0.0) |
Tolerant float comparison. |
isnan, isinf, isfinite |
Tests for special float values. |
fmod, remainder, copysign, modf, frexp, ldexp |
Lower-level float helpers. |
pi, e, tau, inf, nan |
Constants. tau is 2 * pi. |
Usage notes
- Use
isclose()instead of==for floats. Binary floating point cannot represent most decimals exactly, so0.1 + 0.2is not exactly0.3. Setabs_tolwhen comparing against zero, because the default tolerance is relative. math.pow()always returns a float and raisesOverflowErrorfor results too large. The**operator and the built-inpow()keep integers exact.floor,ceilandtruncreturnintand differ for negative numbers;round()uses banker's rounding (ties go to the even number).- Degrees vs radians. The trigonometric functions take radians.
math.sin(90)is not 1; usemath.sin(math.radians(90)). math.nannever equals anything, even itself. Always test withisnan().- Large inputs.
factorial(1000)is a 2568-digit integer and takes microseconds, but passing huge values tocomborfactorialcan use a lot of time and memory. - Sums.
fsum()tracks lost low-order bits and returns the correctly rounded sum. Since 3.12 the built-insum()of floats also uses a compensated algorithm, so the old advice to always preferfsum()matters less, butfsum()remains the guaranteed-accurate choice. - Complex numbers. Use
cmathfor square roots of negative numbers. For arrays of numbers and speed, use NumPy instead of looping overmathfunctions. - Money and exactness. For decimal currency use
decimal; for exact ratios usefractions.