Module 15 · Python Standard Libraries — topic 1 of 13
math
The math module: square roots and powers, rounding functions, factorials and combinations, gcd and lcm, trigonometry, logarithms, constants, and comparing floats safely with isclose.
Learning objectives
- Use common math functions and constants
- Choose the right rounding function
- Compare floating-point numbers safely
Definition
math is a standard module of mathematical functions and constants for real numbers: roots, rounding, logarithms, trigonometry, combinatorics and more. It works with int and float values.
Introduction
Calculating distances, interest, probabilities or angles quickly needs more than + and *. The math module provides the tools.
Why it matters
math functions are fast, precise and well tested. Using them avoids subtle errors in hand-written formulas.
Detailed explanation
Roots and powers: math.sqrt(x), math.isqrt(n) for integer square roots, math.pow(x, y) (always float) and the ** operator.
Rounding: floor rounds down, ceil rounds up, trunc drops the fractional part (towards zero). For negative numbers floor and trunc differ.
Combinatorics: factorial(n), comb(n, k) (choose k of n), perm(n, k) (ordered choices). Number theory: gcd and lcm.
Trigonometry uses radians; convert with math.radians and math.degrees. math.dist and math.hypot compute distances.
Logarithms: math.log(x) natural log, log(x, base), log10, log2; math.exp(x) is e to the power x.
Constants: pi, e, tau, inf and nan. math.isnan and math.isinf test special values.
Floats are approximations, so 0.1 + 0.2 == 0.3 is False. Use math.isclose(a, b) to compare them.
Python's round() uses 'banker's rounding' for exact halves: round(2.5) is 2 and round(3.5) is 4. When you need the familiar 'round half up' behaviour, for example for money, use the decimal module with ROUND_HALF_UP.
math.fsum(values) adds floats more accurately than sum() by tracking lost precision, and math.prod(values) multiplies all the values in an iterable.
Syntax
- Real-number maths functions.
- Trig works in radians.
- Use isclose for floats.
Code examples
Roots and rounding
1.4142135623730951 -3 -2 -2
Counting and number theory
3628800 10 20 12 12
More examples
Distances, angles and growth
5.0 5.0 False True 0.5 45.0 10.24 years to double at 7% True True
Step-by-step explanation
- dist and hypot both find straight-line distances.
- isclose fixes the classic floating-point comparison problem.
- The doubling time uses logarithms: solve 1.07 ** years == 2.
Expected output
Running “Distances, angles and growth” prints exactly:
Practical applications
- Geometry in games and maps.
- Finance formulas and probability calculations.
Best practices
- Use isclose for float comparison.
- Use comb/perm instead of factorial formulas.
- Use decimal for money (not math).
Common mistakes
Degrees in trig functions
Avoid
Prefer
Trigonometric functions expect radians.
Comparing floats with ==
Avoid
Prefer
Tiny rounding errors make exact comparisons unreliable.
Important notes
- cmath provides the same functions for complex numbers.
Key takeaways
- math offers roots, rounding, logs, trig and combinatorics.
- Trig uses radians.
- Compare floats with isclose.
Try it yourself
- 1Compute the area and circumference of a circle for several radii.
- 2How many ways can a team of 3 be chosen from 10 people? Use math.comb.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?