Python math Module
The Python math module provides a large number of commonly used mathematical functions for:
- Trigonometric calculations (sin / cos / tan)
- Logarithmic and exponential operations (log / exp / pow)
- Rounding and truncation (ceil / floor / trunc)
- Combinations, permutations, and factorials (comb / perm / factorial)
- Distance and geometry calculations (dist / hypot / sqrt)
It is one of the most commonly used standard libraries for mathematical operations in Python.
You need to use the math module to accomplish these.
Functions under the math module return floating-point numbers unless explicitly stated otherwise.
If you need to calculate complex numbers, please use the functions of the same name in the cmath module.
To use math functions, you must first import:
View the contents of the math module:
Why Use the math Module?
Section titled “Why Use the math Module?”Although Python comes with many basic operators (such as +, -, *, /), for more complex mathematical calculations, such as:
- Square root
- Trigonometric functions
- Natural logarithm
- Floating-point precision handling
Python math Module Usage Examples
Section titled “Python math Module Usage Examples”The output is:
math Module Constants
Section titled “math Module Constants”| Constant | Description |
| math.e | Returns Euler’s number (2.7182…) |
| math.inf | Returns a positive infinity floating-point number |
| math.nan | Returns a floating-point NaN (not a number) value |
| math.pi | π generally refers to the ratio of circumference to diameter. Pi (3.1415…) |
| math.tau | The mathematical constant τ = 6.283185…, to available precision. Tau is a circle constant equal to 2π, the ratio of circumference to radius. |
math Module Functions
Section titled “math Module Functions”| Function | Description |
| math.acos(x) | Returns the arc cosine of x, with the result ranging from 0 to pi. |
| math.acosh(x) | Returns the inverse hyperbolic cosine of x. |
| math.asin(x) | Returns the arc sine of x, with the result ranging from -pi/2 to pi/2. |
| math.asinh(x) | Returns the inverse hyperbolic sine of x. |
| math.atan(x) | Returns the arc tangent of x, with the result ranging from -pi/2 to pi/2. |
| math.atan2(y, x) | Returns the arc tangent of the given X and Y coordinates, with the result between -pi and pi. |
| math.atanh(x) | Returns the inverse hyperbolic tangent of x. |
| math.ceil(x) | Rounds x up to the nearest integer. |
| math.comb(n, k) | Returns the number of ways to choose k items from n items without repetition and without order. |
| math.copysign(x, y) | Returns a float with the absolute value of x and the sign of y. |
| math.cos() | Returns the cosine of x radians. |
| math.cosh(x) | Returns the hyperbolic cosine of x. |
| math.degrees(x) | Converts angle x from radians to degrees. |
| math.dist(p, q) | Returns the Euclidean distance between two points p and q, given as a sequence of coordinates (or iterable). Both points must have the same dimension. |
| math.erf(x) | Returns the error function of a number. |
| math.erfc(x) | Returns the complementary error function at x. |
| math.exp(x) | Returns e raised to the power of x, Ex, where e = 2.718281… is the base of natural logarithms. |
| math.expm1() | Returns Ex - 1, where e = 2.718281… is the base of natural logarithms. This is usually more accurate than math.e ** x or pow(math.e, x). |
| math.fabs(x) | Returns the absolute value of x. |
| math.factorial(x) | Returns the factorial of x. Raises ValueError if x is not an integer or is negative. |
| math.floor() | Rounds a number down to the nearest integer. |
| math.fmod(x, y) | Returns the remainder of x/y. |
| math.frexp(x) | Returns the mantissa and exponent of x as a pair (m, e). m is a float, e is an integer, such that x == m * 2**e. If x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1. |
| math.fsum(iterable) | Returns the sum of elements in an iterable (tuple, array, list, etc.) as a floating-point value. |
| math.gamma(x) | Returns the gamma function value at x. |
| math.gcd() | Returns the greatest common divisor of the given integer arguments. |
| math.hypot() | Returns the Euclidean norm, sqrt(sum(x**2 for x in coordinates)). This is the length of the vector from the origin to the point given by coordinates. |
| math.isclose(a,b) | Checks whether two values are close to each other. Returns True if a and b are relatively close, otherwise False. |
| math.isfinite(x) | Determines whether x is finite. Returns True if x is neither infinity nor NaN, otherwise False. |
| math.isinf(x) | Determines whether x is infinity. Returns True if x is positive or negative infinity, otherwise False. |
| math.isnan() | Determines whether a number is NaN. Returns True if x is NaN (not a number), otherwise False. |
| math.isqrt() | Rounds the square root down to the nearest integer. |
| math.ldexp(x, i) | Returns x * (2**i). This is essentially the inverse of math.frexp(). |
| math.lgamma() | Returns the natural logarithm of the absolute value of the gamma function at x. |
| math.log(x[, base]) | With one argument, returns the natural logarithm of x (base e). |
| math.log10(x) | Returns the base-10 logarithm of x. |
| math.log1p(x) | Returns the natural logarithm of 1+x (base e). |
| math.log2(x) | Returns the base-2 logarithm of x. |
| math.perm(n, k=None) | Returns the number of ways to choose k items from n items without repetition and with order. |
| math.pow(x, y) | Returns x raised to the power of y. |
| math.prod(iterable) | Calculates the product of all elements in an iterable. |
| math.radians(x) | Converts angle x from degrees to radians. |
| math.remainder(x, y) | Returns the IEEE 754-style remainder of x divided by y. |
| math.sin(x) | Returns the sine of x radians. |
| math.sinh(x) | Returns the hyperbolic sine of x. |
| math.sqrt(x) | Returns the square root of x. |
| math.tan(x) | Returns the tangent of x radians. |
| math.tanh(x) | Returns the hyperbolic tangent of x. |
| math.trunc(x) | Returns the truncated integer part of x, i.e., returns the integer part and removes the decimal part. |
| math.cbrt(x) | Returns the cube root of x. |
| math.exp2(x) | Returns 2 raised to the power of x, i.e., 2**x. |
| math.lcm(*integers) | Returns the least common multiple of multiple integers. |
| math.modf(x) | Returns the fractional and integer parts of x, as a tuple (fractional, integer). |
| math.nextafter(x, y[, steps]) | Returns the value after moving a specified number of floating-point steps from x toward y. |
| math.sumprod(p, q) | Returns the sum of products of corresponding elements from two iterables. |
| math.ulp(x) | Returns the value of the least significant bit of the float x (Unit in the Last Place). |
Version Compatibility Notes
Section titled “Version Compatibility Notes”The following functions were added in Python 3.8+ / 3.9+ / 3.11+:
math.comb(): Python 3.8+math.perm(): Python 3.8+math.lcm(): Python 3.9+math.cbrt(): Python 3.11+math.exp2(): Python 3.11+math.sumprod(): Python 3.12+