Python - Math

Overview

Estimated time: 10–20 minutes

Use the math module for common mathematical functions and constants.

Learning Objectives

  • Use math functions like sqrt, ceil, floor, and trigonometry.
  • Access constants like pi and e.
  • Know when to prefer decimal for precision.

Prerequisites

Examples

import math
print(math.sqrt(16))
print(math.ceil(3.14), math.floor(3.14))
print(math.sin(math.pi / 2))

Common Pitfalls

  • Assuming math functions work on complex numbers; use cmath for complex.
  • Expecting exact decimal arithmetic; use decimal when needed.

Checks for Understanding

  1. Which module handles complex-number math?
  2. How do you round up a number?
Show answers
  1. cmath
  2. math.ceil(x)

Exercises

  1. Given a list of floats, print their squares rounded up to the nearest int.
  2. Compute the hypotenuse given two sides using math.hypot.