Python reference
The math module
Also called: import math · math.sqrt · math.pi · math.atan2 · math.radians
Definition
Python's built-in math module provides constants such as pi and e and functions such as sqrt, sin, cos, atan2, log10, hypot, radians and degrees.
Picture it
Import it once at the top. All trigonometric functions use radians, so wrap degree inputs in math.radians. math.hypot(x, y) is the safe way to compute a distance, and math.atan2(y, x) gives a heading in the right quadrant.
Worked example
What do math.hypot(3, 4), math.degrees(math.atan2(1, 1)) and math.log10(1000) return?
- √(9 + 16) = 5.0
- atan2(1, 1) = π/4 rad = 45.0°
- log10(1000) = 3.0
Answer5.0, 45.0 and 3.0.
✓ The 3 numbers in this answer are recomputed from scratch by an automated test.
In Python
import math
angle = math.radians(30)
print(math.sin(angle)) # 0.49999999999999994Common misconceptions
- Misconception: math.log is the natural logarithm; use math.log10 for base 10.
- Misconception: math works on single numbers; for whole arrays scientists use NumPy.