Python Math
## Python3.x Python math Module
**Python math Module** provides a large number of commonly used mathematical functions, which can be used to complete:
* Trigonometric function calculations (sin / cos / tan)
* Logarithm and exponent operations (log / exp / pow)
* Rounding up and down (ceil / floor / trunc)
* Combinations, permutations and factorial (comb / perm / factorial)
* Distance and geometric 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 complete this.
**math** module functions return floating-point numbers, unless otherwise explicitly stated.
If you need to calculate complex numbers, use the functions with the same name in the cmath module.
To use math functions, you must first import:
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
print(math.sqrt(16))# 4.0
print(math.pow(2,3))# 8.0
print(math.ceil(4.2))# 5
print(math.floor(4.9))# 4
print(math.sin(math.pi/2))# 1.0
The output is:
4.08.0541.0
### math Module Constants
| Constant | Description |
| --- | --- |
| [math.e](#) | Returns Euler's number (2.7182...) |
| [math.inf](#) | Returns positive infinity floating-point number |
| [math.nan](#) | Returns a floating-point value NaN (not a number) |
| [math.pi](#) | Ο generally refers to pi. PI (3.1415...) |
| [math.tau](#) | Mathematical constant Ο = 6.283185..., accurate to available precision. Tau is a circle constant, equal to 2Ο, the ratio of circle circumference to radius. |
### math Module Functions
| Function | Description |
| --- | --- |
| [math.acos(x)](#) | Returns the arccosine of x, the result range is between 0 and pi. |
| [math.acosh(x)](#) | Returns the inverse hyperbolic cosine of x. |
| [math.asin(x)](#) | Returns the arcsine of x, the result range is between -pi/2 and pi/2. |
| [math.asinh(x)](#) | Returns the inverse hyperbolic sine of x. |
| [math.atan(x)](#) | Returns the arctangent of x, the result range is between -pi/2 and pi/2. |
| [math.atan2(y, x)](#) | Returns the arctangent of the given X and Y coordinate values, the result is 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 regard to order. |
| [math.copysign(x, y)](#) | Returns a floating-point number 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)](#) | Converts angle x from radians to degrees. |
| [math.dist(p, q)](#) | Returns the Euclidean distance between points p and q, given as a coordinate sequence (or 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 of x, E x, where e = 2.718281... is the base of the natural logarithm. |
| [math.expm1
YouTip