Math Module in Python are the various modules made in Python for performing useful mathematical operations and complex expressions quite easily, also, without the usual hassles of a complex calculation. These are particularly useful when you have the theory. But, need to use the standard math tricks for your problem. Thus, this math module in Python has many varied and vivid features. Also, there are various functions present in this library and here, I will be bringing out most of them to you.

We thus, need to now know that to perform any mathematical operation in Python we just need to import the “math” module in Python and get our work done by using its various functions.

Various Functions and the task they perform

Various functions in the “math module”Functionality
ceil(a)It, thus, returns the nearest, smallest integer greater than or equal to a
fabs(a)Returns the absolute value of a
factorial(a)Returns the factorial of a
floor(a)Also, returns the largest integer less than or equal to a
fmod(a, b)Returns the remainder when a is divided by b
frexp(a)Thus, returns the mantissa and exponent of a as the pair (m, e)
fsum(iterable)Also, returns an accurate floating point sum of values in the iterable
isfinite(a)Thus, returns True if a is neither an infinity nor a NaN (Not a Number)
isinf(a)Thus, returns True if a is a positive or negative infinity
isnan(a)Returns True if a is not a number (NaN)
trunc(a)Returns the truncated integer value of a
exp(a)Returns e**a
log(x[, base])Also, returns the logarithm of x to the base (defaults to e)
log2(a)Returns the base-2 logarithm of a
log10(a)Returns the base-10 logarithm of a
pow(a, b)Returns a raised to the power b
sqrt(a)Returns the square root of a
acos(a)Returns the arc cosine of a
asin(a)Returns the arc sine of a
atan(a)Returns the arc tangent of a
cos(a)Returns the cosine of a
hypot(a, b)Thus, returns the Euclidean norm, sqrt(a*a + b*b)
sin(a)Returns the sine of a
tan(a)Returns the tangent of a
degrees(a)Converts angle a from radians to degrees also
radians(a)Converts angle a from degrees to radians
tanh(a)Returns the hyperbolic tangent of a
erf(a)Returns the error function at a
erfc(a)Returns the complementary error function at a
lgamma(a)It, thus, returns the natural logarithm of the absolute value of the Gamma function at a
piReturn the value of pi, also, the ratio of circumference of a circle to it’s diameter (3.14159…)

Some Example Codes

import math

print('The Ceiling and the Floor value of 18.92 are: ' + str(math.ceil(23.56)) + ', ' + str(math.floor(23.56)))

print('Absolute value of -96 and 56 are: ' + str(math.fabs(-96)) + ', ' + str(math.fabs(56)))

print('The GCD of 24 and 56 : ' + str(math.gcd(24, 56)))

x = float('nan')
if math.isnan(x):
    print('It is not a number')

x = float('inf')
y = 45
if math.isinf(x):
    print('It is Infinity')


The Ceiling and the Floor value of 18.92 are: 19, 18

Absolute value of -106 and 36 are: 106.0, 36.0

The GCD of 24 and 56 : 8

It is not a number

It is Infinity

Example Code

import math
print('The value of 5^7 is : ' + str(math.pow(5, 8)))
print('Square root of 1600: ' + str(math.sqrt(1600)))
print('The value of 5^e: ' + str(math.exp(5)))
print('The value of Log(125), base 5: ' + str(math.log(125, 5)))
print('The value of Log(512), base 2: ' + str(math.log2(512)))
print('The value of Log(1024), base 10: ' + str(math.log10(1024)))

print('The value of Sin(60 degree): ' + str(math.sin(math.radians(60))))
print('The value of cos(pi): ' + str(math.cos(math.pi)))
print('The value of tan(90 degree): ' + str(math.tan(math.pi/2)))
print('The angle of sin(0.8660254037844386): ' + str(math.degrees(math.asin(0.8660254037844386))))


The value of 5^7 is : 78125.0
Square root of 1600: 40.0
The value of 5^e: 148.4131591025766
The value of Log(625), base 5: 2.0
The value of Log(512), base 2: 9.0
The value of Log(1024), base 10: 3.010299956639812

The value of Sin(60 degree): 0.8660254037844386
The value of cos(pi): -1.0
The value of tan(90 degree): 1.633123935319537e+16
The angle of sin(0.8660254037844386): 59.99999999999999

Thus, from these examples we have seen the various functionalities we can perform. Also, all by using these functions present in the math module in Python. We thus, just simply need to import the math library by using the import math option in Python and we are good to go. And, thus, perform complex mathematical computations and other operations. Lets see another miscellaneous example concerning this, the factorial function.

def fact(n):
    if n < 0:
        return 0
    if n == 0:
        return 1

    factorial = 1
    for i in range(1, n + 1):
        factorial = factorial * i
    return factorial

SUMMING UP — Math Module in Python

So, in this post, we got hold of using the math module in Python. We also saw the various functionalities provided by the math library like sin, cos, tan functions and many more. All these functionalities also, have been demonstrated with the help of ample number of examples.

Thus, by and through this article, I suppose I have made myself pretty clear. But, in case, you still have some doubts lingering. Then, please do write to me in the comments section and I am as always, ever-ready to help you. And, also solve your queries and problems.

Categorized in: