Skip to content
Tutorials Center
My learning

Topics

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).

Python
import math

print(math.sqrt(16), math.pi)

Try it

Python Everyday functions

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.

Python Comparing floats and handling errors

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, so 0.1 + 0.2 is not exactly 0.3. Set abs_tol when comparing against zero, because the default tolerance is relative.
  • math.pow() always returns a float and raises OverflowError for results too large. The ** operator and the built-in pow() keep integers exact.
  • floor, ceil and trunc return int and 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; use math.sin(math.radians(90)).
  • math.nan never equals anything, even itself. Always test with isnan().
  • Large inputs. factorial(1000) is a 2568-digit integer and takes microseconds, but passing huge values to comb or factorial can 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-in sum() of floats also uses a compensated algorithm, so the old advice to always prefer fsum() matters less, but fsum() remains the guaranteed-accurate choice.
  • Complex numbers. Use cmath for square roots of negative numbers. For arrays of numbers and speed, use NumPy instead of looping over math functions.
  • Money and exactness. For decimal currency use decimal; for exact ratios use fractions.