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:
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.
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. |
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.
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. |
In this example, we will see how to calculate the factorial of a number using the math module.
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.
The following example demonstrates how to calculate trigonometric ratios using the math module.
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.
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.
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
Here, we will convert radians to degrees and degrees to Radians by taking input from the user.
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.
x to the power of e, often known as the exponential of a number x, is calculated using the exp() function.
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
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:
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']
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.
We request you to subscribe our newsletter for upcoming updates.