The math module is part of Python’s standard library. It provides functions for common mathematical operations including rounding, trigonometric calculations, logarithms, and constants like pi and e.
Syntax
import math
Key Functions
Numerical Operations
| Function | Signature | Description |
|---|
ceil() | ceil(x) | Returns the smallest integer greater than or equal to x |
floor() | floor(x) | Returns the largest integer less than or equal to x |
sqrt() | sqrt(x) | Returns the square root of x |
pow() | pow(x, y) | Returns x raised to the power of y |
factorial() | factorial(n) | Returns n! (n factorial) |
gcd() | gcd(a, b) | Returns the greatest common divisor of a and b |
fabs() | fabs(x) | Returns the absolute value of x as a float |
Trigonometric Functions
| Function | Signature | Description |
|---|
sin() | sin(x) | Returns the sine of x (x in radians) |
cos() | cos(x) | Returns the cosine of x (x in radians) |
tan() | tan(x) | Returns the tangent of x (x in radians) |
degrees() | degrees(x) | Converts radians to degrees |
radians() | radians(x) | Converts degrees to radians |
Logarithmic and Exponential
| Function | Signature | Description |
|---|
log() | log(x[, base]) | Returns the natural logarithm of x, or to the given base |
log10() | log10(x) | Returns the base-10 logarithm of x |
log2() | log2(x) | Returns the base-2 logarithm of x |
exp() | exp(x) | Returns e raised to the power of x |
Constants
| Constant | Value | Description |
|---|
pi | 3.14159… | The mathematical constant π |
e | 2.71828… | The mathematical constant e |
tau | 6.28318… | The mathematical constant τ (2π) |
inf | ∞ | Positive infinity |
nan | NaN | Not a Number |
Examples
Basic numerical operations
import math
# Rounding
print(math.ceil(4.2)) # 5
print(math.floor(4.8)) # 4
print(math.trunc(4.8)) # 4
# Square root and power
print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0
print(math.pow(27, 1/3)) # 3.0 (cube root)
# Factorial and gcd
print(math.factorial(5)) # 120
print(math.gcd(48, 18)) # 6
Trigonometry
import math
# Sine, cosine, tangent (input in radians)
print(math.sin(math.pi / 2)) # 1.0
print(math.cos(math.pi)) # -1.0
print(math.tan(math.pi / 4)) # 1.0
# Convert between degrees and radians
print(math.degrees(math.pi)) # 180.0
print(math.radians(90)) # π/2 ≈ 1.5708
Logarithms
import math
# Natural logarithm
print(math.log(math.e)) # 1.0
print(math.log(1)) # 0.0
# Base-10 logarithm
print(math.log10(100)) # 2.0
print(math.log10(1000)) # 3.0
# Base-2 logarithm
print(math.log2(8)) # 3.0
print(math.log2(1024)) # 10.0
Using constants
import math
# Calculate area of a circle
radius = 5
area = math.pi * radius ** 2
print(area) # 78.5398...
# Calculate circumference
circumference = 2 * math.pi * radius
print(circumference) # 31.4159...
Common Patterns
Checking if a number is finite or infinite
import math
print(math.isfinite(10)) # True
print(math.isinf(math.inf)) # True
print(math.isnan(math.nan)) # True
Summing with precision
import math
values = [0.1] * 10
total = sum(values) # May not be exactly 1.0
print(total) # 0.9999999999999999
# Use math.fsum for floating-point precision
print(math.fsum(values)) # 1.0
Finding the hypotenuse
import math
# Pythagorean theorem: c = sqrt(a² + b²)
a, b = 3, 4
c = math.hypot(a, b)
print(c) # 5.0
See Also