Python Math Module & Functions | Python Mathematical Functions

Python has many built-in functions that allow the user to carry out several tasks quickly and efficiently. Math is a type of module in Python that allows the user to use several functions for easier operations. Several functions are defined within the module of math. The module is built in the Python programming language and can carry out mathematical calculations and tasks. 

Python math module

General mathematical operations such as additions, subtraction, multiplication, etc., can be carried out through built-in operators such as the +, -, and *. However, operators are not built-in for such cases when the user has to carry out complex mathematical calculations that involve advanced operations like the logarithmic functions, exponential expressions, etc.

Therefore, specific solutions have to be there so that the mathematical operations are carried out quickly. A particular module has been designed in Python, including several functions for carrying out advanced mathematical calculations. This module is known as the “math” module. 

The module has access to mathematical functions that are defined in the C standard. 

The functions defined within the math module, however, cannot be applied to complex numbers. If the user needs to apply these functions in the complex numbers, then the same name can be used from the different module, the “Cmath.” This distinction is made between general mathematics and complex number functions because most users don’t feel like learning mathematics to complex numbers. The underlying functions of the math module are written in CPython, and it follows the C standard.

The availability of the math module allows the users to perform mathematical calculations on the application. A few uses of the module math have been listed below. The functions within the math module can be used for

  • Calculation of the permutations and the combinations through the use of factorial. 
  • Calculation of the pole height through the use of the trigonometric functions.
  • Calculation of the radioactive decay through the use of an exponential function.
  • Calculation of the curve of the suspension bridge through the use of hyperbolic functions.
  • Solving equations such as quadratic equations.
  • Simulation of periodic functions like the light waves, sound waves, and through the use of trigonometric functions.

Separate installation of the math module is not required, as it is already packaged with the release of Python. However, the module needs to be imported for using the functions. The following command can be used for importing the math module.

Learn data science course from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Math module constants

A lot of constants are already predefined in the Python math module. This offers several advantages on being accessed. As these constants are predefined, there is no need for their separate definition in the program, which saves time. The following constants are being defined in the math module.

1. Pi

Pi or π is defined as the ratio of the circumference of a circle to the circle’s diameter. The Number of Pi is not a simple fraction as it is irrational. Also, it has infinite decimal places, but generally, the Number is approximately 22/7, or the value 3.141. in Python, the Pi can be accessed through the math module through the command:

math. pi

The advantages of the Pi lies in the fact that it can be used whenever the Pi is required for calculating any task, like the calculation of the circumference of any circle. In such cases, the circle’s circumference will be 2πr, where the value of Pi can be used.

Also, the area of a circle can be calculated by using the formula πr². Therefore, whenever the user has to do specific calculations in Python that use the value of Pi, then the constant Pi can be imported directly.

2. Tau

Tau is defined as another ratio of the circumference of a circle to the radius of the circle. The constant of Tau is equal to the value of 2π, or an approximated value of 6.28. It is an irrational number similar to that of π. 

Many mathematical calculations require the use of 2π, and in such cases, the tau can be used. 

The constant of tau can be accessed through the command math.tau

Similar to Pi, Tau is also a floating type number. 

3. Euler’s Number

Euler’s Number is another type of constant within the math module which is the base of a natural logarithm. For mathematical calculations such as calculating the decay rate or growth rate, the functions can be used. The value of Euler’s Number is approximated to a value of 2.718. The Number can be imported through the command “math.e”.

4. Infinity

 Whenever the term infinity is mentioned, it cannot be defined as a number; however, it is a concept in mathematics that defines a boundless situation or something that has no end. Infinity can be used in algorithms when the user wants to compare the given value to the absolute minimum or the maximum value. 

In Python, infinity is defined as math.inf.

5. NaN (Not a Number)

Nan is mostly used to define values that are not numerical. This might be due to the result of invalid inputs or if the numerical input gets corrupted. The constant of NaN has been defined in the version of Python 3.5. The NaN can be accessed through math.nan.

A lot of functions and methods are included within the module math. Some of the functions within the module are:

  •  ceil()

 The function ceil() is used for returning the integral value which is the smallest but the value should be greater than that of the Number. 

  •  floor()

The function ceil() is used to return the most integral value but the value should be smaller than the Number. 

  •  fabs()

The function fabs() is used for returning the number’s absolute value.

  •  factorial()

The function factorial within the math module calculates the factorial of a number and returns the factorial. If an integer value is not entered to calculate the factorial, the program results in an error message. 

The working of the functions fabs() and factorial() in the Python math module is shown below.

Explore our Popular Data Science Courses

  •  copysign(a, b)

This function is used for returning a float number. The Number is returned with the value of the parameter ‘a’ having a sign of ‘b’. 

  •  gcd():

The numbers are mentioned in the arguments. For calculating the greatest common divisor for two numbers mentioned in the arguments, the function is used. The function is, however, compatible with version Python 3.5 or a higher version.

A program is shown below, using the two functions copysign(a,b), and gcd() in the Python language. 

  •  math.log()

The method of Python math log, i.e., math.log(), is used to return the value of the natural logarithm of any given number. The calculation is done to the base e. 

  •  math.pow(x,y)

The method math.pow(x,y) returns the value of power of x equal to the value y. In cases where the value of x turns out to be hostile, or the value of y turns out to be a non-integer, then the program will result in a ValueError. 

  •  math.modf(x)

The method math.modf(x) is used to return the fractional part and the integer part for a variable x. 

  •  trunc()

The method trunc() is used to return the truncated value of an integer x.

  •  math.cos()

The function math.cos() is used for returning the cosine value of a number.

  •  math.degrees()

The angle can be converted into degrees from radians through the function math.degrees().

  •  math.erf()

For any number, the error function is returned through the method math.erf().

  •  math.isinf(x)

The function returns an actual value if the input x is negative or positive infinity. Else the function will return a value of False. 

  • after(x,y)

The floating-point Number next to the x is returned that moves towards y. 

Top Data Science Skills You Should Learn

Mathematical Operations with Math Module

The different Python math module functions necessary in theory and number history are covered in this part:

Calculating the Ceiling and the Floor Value

The terms “ceiling value” and “floor value” are the smallest integral value larger than the number and the largest integral value less than the number, respectively. The ceil() and floor() methods can simplify this calculation. The code for it is as follows:

# Python program to show how to use floor() and ceil() functions.  
# importing the math module  
import math
x = 4.346  
# returning the ceiling value of 4.346  
print("The ceiling value of 4.346 is : ", end="")  
print( math.ceil(x) )  
# returning the floor value of 4.346  
print("The floor value of 4.346 is : ", end="")  
print( math.floor(x) )  


The ceiling value of 4.346 is : 5
The floor value of 4.346 is : 4

Calculating the Factorial of the Number

The math.factorial() function can help determine the factorial of an integer in a one-liner code. The Python interpreter provides a message if the number is not integral. The code for this Python math module calculation is as follows:

# Python program to show how to use function() functions.  
# importing the math module  
import math  
x = 6    
# returning the factorial of 6  
print( "The factorial of 6 is : ", math.factorial(x) )  
# passing a non integral number  
    print( "The factorial of 6.5 in: ", math.factorial(6.5) )  
    print( "Cannot calculate factorial of a non-integral number" ) 


The factorial of 6 is : 720

Cannot calculate factorial of a non-integral number

Determining the Absolute Value

You can use the math-.fabs() to return the absolute number of the integer to the function. The code for this Python import math function is as follows:

# Python program to show how to use fabs() functions.  
# importing the math module  
import math  
x = -45  
# returning x's absolute value.  
print( "The absolute value of -45 is: ", math.fabs(x) )  


The absolute value of -45 is: 45.0

Calculating the Exponential

The exp() function can be used to calculate the x to the power of e or the exponential of a number x. The code for it is as follows:

# 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) )


The exponential value of 4 is: 54.598150033144236
The exponential value of -3 is: 0.049787068367863944
The exponential value of 0.0 is: 1.0

Calculating the Power of a Number

The pow() function can determine the value of the power after transforming its inputs into floats:


# Python program to show how to use the pow() function.  
# importing the math module  
import math  
x = 4  
y = 5  
# returning x to the power of y.  
print( f"The value of {x} to the power of {y} is: ", math.pow(x,y


The value of 4 to the power of 5 is: 1024.0 

Read our popular Data Science Articles

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights


The article discussed several functions that are predefined within the module math in Python. Also, many constants have been predefined in the module to make easy and efficient calculations of advanced mathematical problems. In addition to these methods, several other methods also exist for different calculations.

Understanding the different functions and modules in the Python programming language, understanding language is essential. If you are interested, you can check the Advanced Certificate Programme in Data Science course offered by upGrad. The course, designed for working professionals, will provide you with real-life projects and train you in data science. For more queries, you can contact our team of assistance.

Why is the math module used in Python?

The math module is used for carrying out advanced mathematical calculations like trigonometric calculations, exponential problems, etc.

How can the math module be installed?

The math module is a built-in module in Python, and therefore, no separate installation is needed. It gets installed with the Python release.

What does the math module contain?

The math module contains several pre-defined constants like Tau, Pi, etc., and several built-in mathematical calculations methods.

How can Pi be accessed in Python?

Pi can be accessed through the math module using the command math.pi.

Want to share this article?

Be A Master Of Science in Data Science

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks