Python math Module

Python math module helps the user to get direct access to the mathematical functions in their program. Thus, it helps to solve and minimize complex computations.

In order to avail the functionalities of the math module, we need to import it in our code using import math statement.

The math module does not support Complex datatypes. The cmath module serves the functionality for the complex data types.


List of functions in the Python math module

Functions present in the math moduleDescription of the function
factorial(x) Returns the factorial of x
copysign(x, y)Returns x with the sign of y
fabs(x)Returns the absolute value of x
ceil(x)Returns the smallest integer greater than or equal to x.
floor(x)Returns the largest integer less than or equal to x
fmod(x, y)Returns the remainder when x is divided by y
frexp(x)Returns the mantissa and exponent of x as the pair (m, e)
fsum(iterable)Returns an accurate floating point sum of values in the iterable
isfinite(x)Returns True if x is neither an infinity nor a NaN (Not a Number)
isinf(x)Returns True if x is a positive or negative infinity
isnan(x)Returns True if x is a NaN
ldexp(x, i)Returns x * (2**i)
modf(x)Returns the fractional and integer parts of x
trunc(x)Returns the 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
radians(x)Converts angle x from degrees to radians
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
piMathematical constant, the ratio of circumference of a circle to it’s diameter (3.14159…)
emathematical constant e (2.71828…)

Python Math Trigonometric functions

The below code represents some of the trigonometric functions of the math module.

Example:

import math

angle_degree = 60
angle_radian = math.radians(angle_degree)

print('The input angle: ', angle_radian)
print('sin(x): ', math.sin(angle_radian))
print('cos(x): ', math.cos(angle_radian))
print('tan(x): ', math.tan(angle_radian))

Output:

Output Trigonometric Functions
Output-Trigonometric Functions

Python Math Power and Logarithmic Functions

The below code represents some of the logarithmic functions of the math module.

Example:

import math
print('The value of 2^2: ' + str(math.pow(2, 2)))
print('Square root of 121: ' + str(math.sqrt(121)))
print('The value of 8^e: ' + str(math.exp(8)))
print('The value of Log(625) with base 5: ' + str(math.log(625, 5)))
print('The value of Log(444) with base 2: ' + str(math.log2(444)))
print('The value of Log(1000) with base 10: ' + str(math.log10(1000)))

Output:

Output Logarithmic Functions
Output-Logarithmic Functions

Python Math Numeric Representation Functions

The below code represents some of the numeric functions of the math module.

Example:

import math

input = 12.35
print('The Floor value of the given input: ' + str(math.floor(input)))

print('The Ceil value of the given input: ' + str(math.ceil(input)))

a = 20
b = -10

print('The value of a after copying the sign from b: ' + str(math.copysign(a, b)))

s1 = -25
s2 = 25

print('Absolute value of s1 and s2: ' + str(math.fabs(s1)) + ', ' + str(math.fabs(s2)))

my_input = [0,1,2,3,4,5,6,7,8,9,-1]
print('Sum of the elements of the list: ' + str(math.fsum(my_input)))

p = float('nan')
if math.isnan(p):
    print('It is not a number')
q = float('inf')
y = 10
if math.isinf(q):
    print('It is Infinity')
print(math.isfinite(q)) #q is not a finite number
print(math.isfinite(y)) #y is a finite number

Output:

Output Numeric Functions
Output-Numeric Functions

Python Math PI

Python math module provides a constant called pi that can be used in mathematical computations, eg: area of a circle.

Example:

import math

print('Value of pi: ', math.pi)

radius = 2

print('Area of Circle: ', math.pi * (radius ** 2))

Output:

Value of pi: 3.141592653589793
Area of Circle: 12.566370614359172


Conclusion

Thus, in this article, we have implemented most of the functionalities offered by the math module in Python.


References