Python math Module

Last Updated : 6 Jan 2026

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

Python math Module

Let's see a simple example to understand the use of the Math module:

Example: Finding Square Root

Example

Execute Now

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 ValuesDefinition
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

Execute Now

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.FunctionsDescription
1math.sin(x)It provides the sin value of x
2math.cos(x)It gives the cosine value of x.
3math.tan(x)It produces the tangent value of x.
4math.asin(x)It provides the arc sine value of x.
5math.acos(x)This function gives arc cosine value of x.
6math.atan(x)This function gives the arc tangent value of x (in radians).
7math.atan2(y, x)It provides the arc tangent value of y/x in radians.
8math.sinh(x)Hyperbolic sin value.
9math.cosh(x)Hyperbolic cosine value.
10math.tanh(x)Hyperbolic tangent value.
11math.asinh(x)Inverse hyperbolic sine value.
12math.acosh(x)Inverse hyperbolic cosine value.
13math.atanh(x)Inverse hyperbolic tangent value.
14math.degrees(x)It converts radians to degrees.
15math.radians(x)It converts degrees to radians.
16math.exp(x)It gives ex.
17math.expm1(x)It gives ex-1.
18math.log(x, base)It gives the value of log with its base.
19math.log10(x)It gives the Base 10 logarithm.
20math.log1p(x)log(1+x).
21math.log2(x)It gives the Base 2 logarithm.
22math.pow(x, y)It gives xy.
23math.sqrt(x)This generates the square root of the number.
24math.fabs(x)It gives the absolute value.
25math.factorial(n)It gives the factorial of number n.
26math.comb(x, y)It produces the combinations.
27math.perm(x, y)It produces the permutations.
28math.isfinite(n)It checks if n is finite.
29math.isinf(n)It checks if n is infinite.
30math.gamma(x)It returns the gamma function of the argument.
31math.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

Execute Now

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

Execute Now

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

Execute Now

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

Execute Now

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

Execute Now

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

Execute Now

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.