Python Math Module (2024)
The Python math module provides numerous mathematical functions for floating-point operations.
All functions under the math module return floating-point values unless otherwise specified.
If you need to calculate with complex numbers, use the same-named functions in the cmath module.
To use the math functions, you must first import the module:
import math
You can check the contents of the math module using the following command:
>>> import math >>> dir(math) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
Constants in the math Module
| Constant | Description | 
|---|---|
| math.e | Returns Euler's number (2.7182...) | 
| math.inf | Returns positive infinity | 
| math.nan | Returns NaN (not a number) | 
| math.pi | π, representing the ratio of a circle's circumference to its diameter (3.1415...) | 
| math.tau | Mathematical constant τ = 6.283185..., which is equal to 2π, representing the ratio of a circle's circumference to its radius. | 
Methods in the math Module
| Method | Description | 
|---|---|
| math.acos(x) | Returns the arc cosine of x, result between 0 and π. | 
| math.acosh(x) | Returns the inverse hyperbolic cosine of x. | 
| math.asin(x) | Returns the arc sine of x, result between -π/2 and π/2. | 
| math.asinh(x) | Returns the inverse hyperbolic sine of x. | 
| math.atan(x) | Returns the arc tangent of x, result between -π/2 and π/2. | 
| math.atan2(y, x) | Returns the arc tangent of y/x, result between -π and π. | 
| math.atanh(x) | Returns the inverse hyperbolic tangent of x. | 
| math.ceil(x) | Rounds xupwards to the nearest integer. | 
| math.comb(n, k) | Returns the number of ways to choose kitems fromnwithout repetition and without order. | 
| math.copysign(x, y) | Returns a float with the magnitude of xbut the sign ofy. | 
| math.cos(x) | Returns the cosine of xradians. | 
| math.cosh(x) | Returns the hyperbolic cosine of x. | 
| math.degrees(x) | Converts angle xfrom radians to degrees. | 
| math.dist(p, q) | Returns the Euclidean distance between points pandq. Points must have the same dimensions. | 
| math.erf(x) | Returns the error function of x. | 
| math.erfc(x) | Returns the complementary error function at x. | 
| math.exp(x) | Returns eraised to the power ofx. | 
| math.expm1(x) | Returns e^x - 1, whereeis Euler's number. | 
| math.fabs(x) | Returns the absolute value of x. | 
| math.factorial(x) | Returns the factorial of x. Raises a ValueError ifxis not an integer or is negative. | 
| math.floor(x) | Rounds xdown to the nearest integer. | 
| math.fmod(x, y) | Returns the remainder when xis divided byy. | 
| math.frexp(x) | Returns the mantissa and exponent of xas a pair(m, e). | 
| math.fsum(iterable) | Returns the sum of all elements in an iterable as a floating-point number. | 
| math.gamma(x) | Returns the gamma function value at x. | 
| math.gcd(a, b) | Returns the greatest common divisor of integers aandb. | 
| math.hypot(*coordinates) | Returns the Euclidean norm, sqrt(sum(x**2 for x in coordinates)). | 
| math.isclose(a, b) | Returns Trueifaandbare close in value, otherwiseFalse. | 
| math.isfinite(x) | Returns Trueifxis finite, otherwiseFalse. | 
| math.isinf(x) | Returns Trueifxis infinite, otherwiseFalse. | 
| math.isnan(x) | Returns Trueifxis NaN (not a number), otherwiseFalse. | 
| math.isqrt(x) | Returns the integer square root of x. | 
| math.ldexp(x, i) | Returns x * (2**i). Essentially the inverse ofmath.frexp(). | 
| math.lgamma(x) | Returns the natural logarithm of the absolute value of the gamma function at x. | 
| math.log(x[, base]) | Returns the logarithm of xto the givenbase(natural log if not specified). | 
| math.log10(x) | Returns the base-10 logarithm of x. | 
| math.log1p(x) | Returns the natural logarithm of 1+x. | 
| math.log2(x) | Returns the base-2 logarithm of x. | 
| math.perm(n, k=None) | Returns the number of ways to choose kitems fromnwith repetition and with order. | 
| math.pow(x, y) | Returns xraised to the power ofy. | 
| math.prod(iterable) | Returns the product of all elements in an iterable. | 
| math.radians(x) | Converts angle xfrom degrees to radians. | 
| math.remainder(x, y) | Returns the remainder of x / y, following IEEE 754 standards. | 
| math.sin(x) | Returns the sine of xradians. | 
| 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 xradians. | 
| math.tanh(x) | Returns the hyperbolic tangent of x. | 
| math.trunc(x) | Returns the truncated integer value of x. Removes the decimal part. | 
The math module in Python provides a robust set of functions and constants for performing mathematical operations. 
Let me know if you need further adjustments!
 
  
 