In Python, the math module is a built-in module that allows us to use mathematical tools and helps us to perform complex mathematical calculations, logarithms, exponentials, and other arithmetic operations.
To read more Python Modules

Let’s see a simple example to understand the use of the Math module:
Example: Finding Square Root
Example
#Importing the math module
import math
#Entering the number
num = int(input("Enter a number:"))
#We are sqrt() function to find the square root of the number
print(math.sqrt(num)) # Uses math module
Output:
Enter a number:25
The square root of the number is: 5.0
Explanation
In the above example, we have imported the math module because it has built-in function sqrt() to find the square root.
Constants in math Module
The math module provides the values of several constants that we regularly use in mathematical contexts.
For example, to find the area of a circle, we have to use pi because the formula is pi*r2, where r is the radius of the circle.
So, the math module makes it easier for us by providing built-in constant values.
| Constant Values | Definition |
|---|---|
| Pi(π) | The value of pi(π) is 22/7 or 3.14 mathematically, we use math.pi to get the most accurate value. |
| Euler’s number(e) | The mathematically accepted value of Euler’s number, which is returned by math.e is 2.718281828459045. |
| Tau(τ) | The value of Tau, which is returned by math.tau is 6.283185307179586. |
| Infinity(∞) | The Mathematical value of Infinity, which is returned by math.inf, is inf. The infinity can be both Positive and Negative. |
| Not a Number(NaN) | The value of Not a Number(NaN), which is returned by math.nan, is nan, which is not a valid number. |
Example: Printing Constant Values
Example
#importing math module
import math
#Printing constant values
print("Value of Pi Constant=", math.pi) #Pi's constant
print("Value of Euler's Constant=", math.e) #Euler's constant
print("Value of Tau's Constant=", math.tau) #Tau's constant
print("Value of Infinity =", math.inf) #Infinity
print("Value of Not a Number =", math.nan) #Not a Number
Output:
Value of Pi Constant 3.141592653589793
Value of Euler's Constant 2.718281828459045
Value of Tau's Constant 6.283185307179586
Value of Infinity = inf
Value of Not a Number = nan
Explanation
In the above example, we have imported the math module and printed the values of various mathematical constants using math.constant_name.
Python math Module Functions
As we know, the Python math module allows us to perform advanced mathematical calculations, such as trigonometric functions, logarithms, and exponentials. For performing the complex calculation math module provides the following built-in functions.
| S. N. | Functions | Description |
|---|---|---|
| 1 | math.sin(x) | It provides the sin value of x |
| 2 | math.cos(x) | It gives the cosine value of x. |
| 3 | math.tan(x) | It produces the tangent value of x. |
| 4 | math.asin(x) | It provides the arc sine value of x. |
| 5 | math.acos(x) | This function gives arc cosine value of x. |
| 6 | math.atan(x) | This function gives the arc tangent value of x (in radians). |
| 7 | math.atan2(y, x) | It provides the arc tangent value of y/x in radians. |
| 8 | math.sinh(x) | Hyperbolic sin value. |
| 9 | math.cosh(x) | Hyperbolic cosine value. |
| 10 | math.tanh(x) | Hyperbolic tangent value. |
| 11 | math.asinh(x) | Inverse hyperbolic sine value. |
| 12 | math.acosh(x) | Inverse hyperbolic cosine value. |
| 13 | math.atanh(x) | Inverse hyperbolic tangent value. |
| 14 | math.degrees(x) | It converts radians to degrees. |
| 15 | math.radians(x) | It converts degrees to radians. |
| 16 | math.exp(x) | It gives ex. |
| 17 | math.expm1(x) | It gives ex-1. |
| 18 | math.log(x, base) | It gives the value of log with its base. |
| 19 | math.log10(x) | It gives the Base 10 logarithm. |
| 20 | math.log1p(x) | log(1+x). |
| 21 | math.log2(x) | It gives the Base 2 logarithm. |
| 22 | math.pow(x, y) | It gives xy. |
| 23 | math.sqrt(x) | This generates the square root of the number. |
| 24 | math.fabs(x) | It gives the absolute value. |
| 25 | math.factorial(n) | It gives the factorial of number n. |
| 26 | math.comb(x, y) | It produces the combinations. |
| 27 | math.perm(x, y) | It produces the permutations. |
| 28 | math.isfinite(n) | It checks if n is finite. |
| 29 | math.isinf(n) | It checks if n is infinite. |
| 30 | math.gamma(x) | It returns the gamma function of the argument. |
| 31 | math.lgamma(x) | It returns the natural log of the gamma function. |
Example 1: Finding the Factorial of a Number
In this example, we will see how to calculate the factorial of a number using the math module.
Example
#importing the math module
import math
n = int(input("Enter a number: "))
#using math.factorial() function
print("The factorial of the entered number is: ",math.factorial(n))
Output:
Enter a number: 5
The factorial of the entered number is: 120
Explanation
In this example, we calculated the factorial of a number using math.factorial(n) function where n is the number entered by the user.
Performing Trigonometric Operations
The following example demonstrates how to calculate trigonometric ratios using the math module.
Example: Using Trigonometric Operations
Example
#importing the math module
import math
#taking input from the user
n = int(input("Enter a number: "))
#returning the values of various trigonometric functions
print("The Sine value is: ",math.sin(n))
print("The Cosine value is: ",math.cos(n))
print("The Tan value is: ",math.tan(n))
Output:
Enter a number: 90
The Sine value is: 0.8939966636005579
The Cosine value is: -0.4480736161291701
The Tan value is: -1.995200412208242
Explanation
In the above example, we have calculated the values of some trigonometric functions using the sin, cos and tan functions in the math module.
Permutation and Combination
In the following example, we will determine the number of ways to choose a given number of balls from a bag of balls. In order to find the combination, we will be using the math.comb() function.
Example: Ways of Choosing Balls from a Bag
Example
#importing the math module
import math
# given data
n = 10 # total no of balls
r = 4 # number of balls to be selected
# Finding the number of combinations using math.comb()
no_of_ways = math.comb(n, r)
# printing result
print("Given Data:")
print("Total number of Balls in the bag:", n)
print("Number of Balls to be selected:", r)
print("Total number of Ways to select", r, "balls from the bag of", n, "balls:")
print(no_of_ways)
Output:
Given Data:
Total number of Balls in the bag: 10
Number of Balls to be selected: 4
Total number of Ways to select 4 balls from the bag of 10 balls:
210
Explanation
Here we calculated the number of ways to choose 4 balls from the bag of 10 balls using math.comb() function
Example 4: Degree to Radians and Vice-Versa Conversion
Here, we will convert radians to degrees and degrees to Radians by taking input from the user.
Example
#importing the math module
import math
# Taking input in radians and converting to degrees
rad = float(input("Enter the angle in radians: "))
print("Radians to Degrees =", math.degrees(rad))
# Taking input in degrees and converting to radians
deg = float(input("Enter the angle in degrees: "))
print("Degrees to Radians =", math.radians(deg))
Output:
Enter the angle in radians: 6.28318530718
Radians to Degrees = 360.0000000000237
Enter the angle in degrees: 360
Degrees to Radians = 6.283185307179586
Explanation
In this example, we have used math.degrees() and math.radians() functions to convert radians to degrees and vice versa, receiving input from the user.
Calculating Exponential
x to the power of e, often known as the exponential of a number x, is calculated using the exp() function.
Example
# Python program to show how to use the exp() function.
# importing the math module
import math
# declaring some value
num1 = 4
num2 = -3
num3 = 0.00
# passing above values to the exp() function
print( f"The exponenetial value of {num1} is: ", math.exp(num1) )
print( f"The exponenetial value of {num2} is: ", math.exp(num2) )
print( f"The exponenetial value of {num3} is: ", math.exp(num3) )
Output:
The exponenetial value of 4 is: 54.598150033144236
The exponenetial value of -3 is: 0.049787068367863944
The exponenetial value of 0.0 is: 1.0
Using dir() Function
A sorted list of strings comprising the identifiers of the functions defined by a module is what the built-in method dir() delivers.
The list includes the names of modules, each specified constants, functions, and methods. Here is a straightforward illustration:
Example
# Importing the math module
import math
functions = dir(math)
print( functions )
Output:
['__doc__', '__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']
Conclusion
The Python Math Module is a built-in module that allows us to perform advanced mathematical calculations, such as trigonometric functions, logarithms, and exponentials. We got to know about various types of functions, such as exponential and logarithmic functions. We learned about Constant values and their applications.
Leave a Reply