Python math Module
Python math Moduleprovides a large number of commonly used math functions, which can be used to accomplish:
- 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 math module to accomplish it.
math Functions under the module all return float values, unless otherwise specified.
If you need to compute complex numbers, use the functions with the same name in the cmath module.
To use math functions, you must import first:
import math
View the contents of the math module:
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'
]
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
import
math
(
math
.
sqrt
(
16
)
)
4.0
(
math
.
pow
(
2
,
3
)
)
8.0
(
math
.
ceil
(
4.2
)
)
5
(
math
.
floor
(
4.9
)
)
4
(
math
.
sin
(
math
.
pi
/
2
)
)
1.0
The output is:
4.0
8.0
5
4
1.0
math Module Constants
| Constant | Description |
|---|---|
| math.e | Returns Euler's number (2.7182...) |
| math.inf | Returns positive infinity as a float |
| math.nan | Returns a float value NaN (not a number) |
| math.pi | π generally refers to the circumference ratio. Pi (3.1415...) |
| math.tau | Mathematical constant τ = 6.283185..., accurate to available precision. Tau is a circle constant equal to 2π, the ratio of a circle's circumference to its radius. |
math Module Functions
| function | Description |
|---|---|
| math.acos(x) | Returns the arc cosine of x, with the result in the range 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 in the range -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 in the range -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) | Round x up to the nearest integer |
| math.comb(n, k) | Returns the total number of ways to choose k items from n items without repetition and without order. |
| math.copysign(x, y) | Returns a float based on 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) | Convert angle x from radians to degrees. |
| math.dist(p, q) | Returns the Euclidean distance between points p and q, given as a sequence of coordinates (or an iterable). The two 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 x, e^x, where e = 2.718281... is the base of the natural logarithm. |
| math.expm1() | Returns e^x - 1, e raised to the power x, where e = 2.718281... is the base of the natural logarithm. This is usually more precise 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. A ValueError is raised if x is not an integer or is negative. |
| math.floor() | Round 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 exactly x == m * 2**e. If x is zero, (0.0, 0) is returned; otherwise 0.5 <= abs(m) < 1 。 |
| math.fsum(iterable) | Returns the sum of elements in an iterable (tuple, array, list, etc.) as a float 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 given point. |
| math.isclose(a,b) | Check whether two values are close to each other; if a and b are close, return True, otherwise return False. |
| math.isfinite(x) | Determine whether x is finite; if x is neither infinity nor NaN, return True, otherwise return False. |
| math.isinf(x) | Determine whether x is infinite; if x is positive or negative infinity, return True, otherwise return False. |
| math.isnan() | Determine whether a number is NaN; if x is NaN (not a number), return True, otherwise return False. |
| math.isqrt() | Round the square root down to the nearest integer |
| math.ldexp(x, i) | Returns x * (2**i). This is basically the inverse of the function 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 (base e) of x. |
| math.log10(x) | Returns the base-10 logarithm of x. |
| math.log1p(x) | Returns the natural logarithm (base e) of 1+x. |
| math.log2(x) | Returns the base-2 logarithm of x |
| math.perm(n, k=None) | Returns the total 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 y. |
| math.prod(iterable) | Compute the product of all elements in an iterable. |
| math.radians(x) | Convert 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 discards the fractional part |
| math.cbrt(x) | Returns the cube root of x. |
| math.exp2(x) | Returns 2 raised to the power 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 obtained by advancing x by the specified number of float steps toward y. |
| math.sumprod(p, q) | Returns the sum of the products of corresponding elements of two iterables. |
| math.ulp(x) | Returns the smallest positive float unit (Unit in the Last Place) of x. |
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+