Skip to content

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.

Beginner 8 min read·Lesson 222 of 306 in Python Programming

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

Python
import math
math.sqrt(2)
math.floor(-2.5), math.ceil(-2.5)
math.comb(5, 2)
math.isclose(0.1 + 0.2, 0.3)
  • Real-number maths functions.
  • Trig works in radians.
  • Use isclose for floats.

Code examples

Roots and rounding

Python
import math
print(math.sqrt(2))
print(math.floor(-2.5), math.ceil(-2.5), math.trunc(-2.5))
Output
1.4142135623730951
-3 -2 -2

Counting and number theory

Python
import math
print(math.factorial(10), math.comb(5, 2), math.perm(5, 2), math.gcd(48, 36), math.lcm(4, 6))
Output
3628800 10 20 12 12

More examples

Distances, angles and growth

Python
import math
print(math.dist((1, 2), (4, 6)), math.hypot(3, 4))
print(0.1 + 0.2 == 0.3, math.isclose(0.1 + 0.2, 0.3))
angle = math.radians(30)
print(round(math.sin(angle), 4), round(math.degrees(math.pi / 4), 1))
years = math.log(2) / math.log(1.07)
print(f"{years:.2f} years to double at 7%")
print(math.inf > 10 ** 100, math.isnan(float("nan")))
Output
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:

TextOutput
5.0 5.0
False True
0.5 45.0
10.24 years to double at 7%
True True

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

math.sin(30)

Prefer

math.sin(math.radians(30))

Trigonometric functions expect radians.

Comparing floats with ==

Avoid

if total == 0.3:

Prefer

if math.isclose(total, 0.3):

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

  1. 1
    Compute the area and circumference of a circle for several radii.
  2. 2
    How 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?
Practise this topic