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.
math module 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