Python offers powerful tools for performing mathematical operations—ranging from simple arithmetic to advanced functions like trigonometry and logarithms. Whether you’re working on data analysis, game development, or scientific computing, this guide will help you navigate the essential math functions in Python.
Why Use Math Functions?
Python’s math functions allow you to:
- Perform precise calculations.
- Handle floating-point numbers with care.
- Work with constants like π and e.
- Simplify complex equations with built-in methods.
They come in two flavors:
- Built-in functions – always available.
-
mathmodule functions – import the math module to access them.
📌 Built-in Math Functions
| Function | Description |
|---|---|
abs(x) |
Absolute value |
round(x, n) |
Round to n decimal places |
pow(x, y) |
x to the power of y |
max(x, y, ...) |
Largest value |
min(x, y, ...) |
Smallest value |
sum(iterable) |
Sum of elements |
math Module Functions (import math)
To unlock more mathematical power, use Python’s math module:
import math
Note:
The following are the most-used functions only, checkout the complete list on docs.python.org.
🔢 Number Theory & Rounding
| Function | Description |
|---|---|
math.ceil(x) |
Round up |
math.floor(x) |
Round down |
math.trunc(x) |
Truncate fractional part |
math.isqrt(x) |
Integer square root |
math.factorial(x) |
x! |
math.fmod(x, y) |
Modulo with floats |
math.remainder(x, y) |
IEEE remainder |
math.copysign(x, y) |
x with the sign of y |
📐 Trigonometry
| Function | Description |
|---|---|
math.sin(x) |
Sine (x in radians) |
math.cos(x) |
Cosine |
math.tan(x) |
Tangent |
math.asin(x) |
Inverse sine |
math.acos(x) |
Inverse cosine |
math.atan(x) |
Inverse tangent |
math.atan2(y, x) |
Arctangent of y/x |
math.hypot(x, y, ...) |
Euclidean norm √(x² + y² + …) |
math.degrees(x) |
Convert radians to degrees |
math.radians(x) |
Convert degrees to radians |
📊 Exponentials & Logarithms
| Function | Description |
|---|---|
math.exp(x) |
e ** x |
math.expm1(x) |
e ** x – 1 (more accurate for small x) |
math.log(x, base) |
Logarithm (default base e) |
math.log2(x) |
Base-2 logarithm |
math.log10(x) |
Base-10 logarithm |
math.pow(x, y) |
x to the power y (float version) |
🧬 Special Functions
| Function | Description |
|---|---|
math.gamma(x) |
Gamma function |
math.lgamma(x) |
Log(abs(gamma(x))) |
math.erf(x) |
Error function |
math.erfc(x) |
Complementary error function |
📏 Floating Point & Precision
| Function | Description |
|---|---|
math.fabs(x) |
Absolute float value |
math.isclose(a, b) |
Are a and b close? |
math.dist(p, q) |
Distance between points |
math.nextafter(x, y) |
Smallest float > x toward y |
math.ulp(x) |
Unit in the last place |
🔍 Finite/Infinite/NaN Checks
| Function | Description |
|---|---|
math.isfinite(x) |
Is finite? |
math.isinf(x) |
Is infinite? |
math.isnan(x) |
Is NaN? |
🧮 Constants
| Constant | Description |
|---|---|
math.pi |
π ≈ 3.14159 |
math.e |
Euler’s number ≈ 2.71828 |
math.tau |
τ = 2π ≈ 6.28318 |
math.inf |
Infinity |
math.nan |
Not a number |
⚙️ Practical Example
Here’s a real-world demo that uses almost every function listed above:
You can run it and see the output online here: pythononline.net/#ka0YkL
import math
# Built-in functions
print("abs(-7):", abs(-7))
print("round(3.14159, 2):", round(3.14159, 2))
print("pow(2, 5):", pow(2, 5))
print("max(1, 5, 3):", max(1, 5, 3))
print("min(1, 5, 3):", min(1, 5, 3))
print("sum([1, 2, 3]):", sum([1, 2, 3]))
# math module
print("math.ceil(2.3):", math.ceil(2.3))
print("math.floor(2.7):", math.floor(2.7))
print("math.trunc(3.9):", math.trunc(3.9))
print("math.isqrt(10):", math.isqrt(10))
print("math.factorial(5):", math.factorial(5))
print("math.fmod(10, 3):", math.fmod(10, 3))
print("math.remainder(10, 3):", math.remainder(10, 3))
print("math.copysign(-2, 3):", math.copysign(-2, 3))
# Trigonometry
angle = math.pi / 4
print("math.sin(angle):", math.sin(angle))
print("math.cos(angle):", math.cos(angle))
print("math.tan(angle):", math.tan(angle))
print("math.asin(0.5):", math.asin(0.5))
print("math.acos(0.5):", math.acos(0.5))
print("math.atan(1):", math.atan(1))
print("math.atan2(1, 1):", math.atan2(1, 1))
print("math.hypot(3, 4):", math.hypot(3, 4))
print("math.degrees(math.pi):", math.degrees(math.pi))
print("math.radians(180):", math.radians(180))
# Logs & exponentials
print("math.exp(2):", math.exp(2))
print("math.expm1(1e-5):", math.expm1(1e-5))
print("math.log(100, 10):", math.log(100, 10))
print("math.log2(8):", math.log2(8))
print("math.log10(1000):", math.log10(1000))
print("math.pow(3, 3):", math.pow(3, 3))
# Special functions
print("math.gamma(5):", math.gamma(5))
print("math.lgamma(5):", math.lgamma(5))
print("math.erf(1):", math.erf(1))
print("math.erfc(1):", math.erfc(1))
# Floating point helpers
print("math.fabs(-3.5):", math.fabs(-3.5))
print("math.isclose(1.000001, 1.0, rel_tol=1e-5):", math.isclose(1.000001, 1.0, rel_tol=1e-5))
print("math.dist([0, 0], [3, 4]):", math.dist([0, 0], [3, 4]))
print("math.nextafter(1, 2):", math.nextafter(1, 2))
print("math.ulp(1.0):", math.ulp(1.0))
# Checks
print("math.isfinite(1.0):", math.isfinite(1.0))
print("math.isinf(math.inf):", math.isinf(math.inf))
print("math.isnan(math.nan):", math.isnan(math.nan))
# Constants
print("math.pi:", math.pi)
print("math.e:", math.e)
print("math.tau:", math.tau)
print("math.inf:", math.inf)
print("math.nan:", math.nan)
🧩 Tips & Notes
-
math.sqrt()returns a float, even for perfect squares. -
math.factorial()only works with non-negative integers. - Trigonometric functions like
math.sin()use radians, not degrees. Convert degrees using:
math.radians(45) # Converts 45° to radians
-
math.fabs()always returns a float, unlikeabs()which preserves the original type.
🆚 math vs. numpy
| Feature | math |
numpy |
|---|---|---|
| Speed | Fast for single values | Fast for arrays |
| Array support | ❌ No | ✅ Yes |
| SciPy integration | ❌ No | ✅ Seamless |
| Precision | Good | Excellent with dtypes |
Use
mathfor simple scalar math. Switch tonumpyfor bulk operations or scientific computing.