# Python math Module

The Python math module is a built-in standard library component that implements mathematical calculations in projects.

The  math module deals with floating-point numbers. If you need to work with complex numbers, use the cmath module.

To use mathematical functions under this module, you must import the module using import math.

import math

## Example

import math

print(math.sqrt(100));  # Calculate the square root

Output

10.0

Here’s an overview of constants and methods in the math module:

## Math Constants

 Constants Description math.pi Returns PI (3.14159…) math.e Returns Euler’s number (2.7182…) math.nan Returns a floating-point “Not a Number” (NaN) value math.inf Returns floating-point positive infinity. (Use -math.inf for negative infinity.) math.tau Returns tau which is approximately 6.283185307179586

### Example

import math

print('The value of PI:',math.pi)
print ('The value of e:',math.e)

Output

The value of PI: 3.141592653589793
The value of e: 2.718281828459045

## Math Methods

Function Description
ceil(x) Returns the smallest integer value greater than or equal to x.
copysign(x, y) Returns x with a sign of y
fabs(x) Returns the absolute value of x
factorial(x) Returns the factorial of x
floor(x) Returns the largest integer less than or equal to x
fmod(x, y) Returns a remainder when x is divided by y
frexp(x) Returns a mantissa and exponent of x as the pair (m, e)
fsum(iterable) Returns the accurate floating-point sum of values in the iterable
isfinite(x) Returns True if x is neither infinity nor a NaN (Not a Number)
isinf(x) Returns True if x is the positive or negative infinity
isnan(x) Returns True if x is the NaN
ldexp(x, i) Returns x * (2**i)
modf(x) Returns fractional and integer parts of x
trunc(x) Returns a truncated integer value of x
exp(x) Returns e**x
expm1(x) Returns e**x – 1
log(x[, base]) Returns the logarithm of x to the base (defaults to e)
log1p(x) Returns the natural logarithm of 1+x
log2(x) Returns the base-2 logarithm of x
log10(x) Returns the base-10 logarithm of x
pow(x, y) Returns x raised to the power y
sqrt(x) Returns the square root of x
acos(x) Returns the arc cosine of x
asin(x) Returns the arc sine of x
atan(x) Returns the arc tangent of x
atan2(y, x) Returns atan(y / x)
cos(x) Returns the cosine of x
hypot(x, y) Returns the Euclidean norm, sqrt(x*x + y*y)
sin(x) Returns the sine of x
tan(x) Returns the tangent of x
degrees(x) Converts angle x from radians to degrees
acosh(x) Returns the inverse hyperbolic cosine of x
asinh(x) Returns the inverse hyperbolic sine of x
atanh(x) Returns the inverse hyperbolic tangent of x
cosh(x) Returns the hyperbolic cosine of x
sinh(x) Returns the hyperbolic cosine of x
tanh(x) Returns the hyperbolic tangent of x
erf(x) Returns the error function at x
erfc(x) Returns the complementary error function at x
gamma(x) Returns the Gamma function at x
lgamma(x) Returns the natural logarithm of the absolute value of the Gamma function at x

### Example

import math

print(math.factorial(5));  # Calculate the factorial

Output

120

That’s it for this tutorial.

This site uses Akismet to reduce spam. Learn how your comment data is processed.