1. Home

Python Tutorials - Elevate Your Coding Skills with Comprehensive Tutorials

Discover Python tutorials covering everything from the basics to advanced topics. Whether you're new to programming or looking to level up your skills, our tutorials will guide you every step of the way

  • 201 Lessons
  • 33 Hours

Tutorial Playlist

200 Lessons

Math Module in Python

Updated on 22/05/20244,404 Views


The Math Module in Python is a powerful toolbox for various math operations in Python. It handles everything from basic arithmetic to advanced functions like trigonometry and logarithms. This article explores its features and constants, showing how it can be used for mathematical tasks in Python.


Math Module in Python is like a magical toolbox for all things mathematical. It's like having a super-smart math assistant in your code, helping with everyday math like addition and subtraction but also handling fancy tricks like trigonometry, logarithms, and unique mathematical constants. This module is your secret weapon whether you're a coding newbie or a seasoned pro working on scientific or engineering projects. It's the math wizard that supercharges Python's number-crunching capabilities.

What is a Math Module in Python?

The Python Math Module is like a toolbox of math tools that come with Python. These tools help you do math stuff, like adding and subtracting, and also fancier things, like trigonometry and logarithms.


  • math.e returns Euler's number: 2.718281828459045
  • math.pi returns the value of pi: 3.141592653589793
  • math.tau returns tau, the ratio of a circle's circumference to its radius: 6.283185307179586
  • math.inf represents positive infinity, and -math.inf represents negative infinity
  • math.nan represents a floating-point NaN (Not a Number) value

Mathematical Operations:

  • Calculating the ceiling and floor value with math.ceil() and math.floor()
  • Finding the factorial of a number with math.factorial()
  • Calculating the greatest common divisor with math.gcd()
  • Finding the absolute value with math.fabs()
  • Calculating exponential values with math.exp()

The Math Module simplifies complex mathematical operations in Python, making it a powerful tool for scientific and financial applications.

What is the Civision Concept in the Python Math Module?

The Python math module comes built-in with Python and is a handy toolbox for doing all sorts of math stuff. It's like a virtual math wizard that helps you crunch numbers and use special math values in your Python programs. Here, we will explore the concept of division in the Python math module and provide examples:

Division in Python Math Module

Division in the Python math module is performed using regular Python division operators (/, //, and %). The math module doesn't introduce any new division operators or functions specifically for division operations. Instead, it relies on Python's built-in division operators for numerical operations.

1. Floating-Point Division (/):

The / operator is used for regular floating-point division in Python. It returns a floating-point result, including the decimal part.

a = 10
b = 3
result = a / b
print("Floating-Point Division:", result)


Floating-Point Division: 3.3333333333333335

2. Integer Division (//):

The // operator helps with dividing integers. It gives you the biggest whole number that is less than or equal

a = 10
b = 3
result = a // b
print("Integer Division:", result)


Integer Division: 3

3. Modulus (%):

The % operator calculates the remainder of the division of two numbers.

a = 10
b = 3
result = a % b
print("Modulus (Remainder):", result)


Modulus (Remainder): 1

In the code examples above, we used standard Python operators to perform division operations. The math module is more focused on providing mathematical functions and constants rather than introducing new division operators.

These division concepts are part of Python's core functionality. They can be used in combination with the functions and constants provided by the math module to perform a wide range of mathematical calculations.

Constants Provided By The Math Module

Here are the explanations and examples of the constants provided by the math module in Python with example:

1. Euler’s Number

Euler's number, sometimes written as "math.e," is a fundamental mathematical constant. It's approximately 2.718281828459045. This number is essential for natural logarithms and has a key role in many mathematical and scientific calculations.



2. Pi

Pi, written as math.pi, is a vital mathematical number. It's roughly 3.141592653589793, and it helps us in math when we're dealing with circles and angles. Pi shows us how many times a circle's circumference goes around its diameter. We use it in different math problems involving shapes and angles.



3. Tau

Tau, represented by math.tau, is a constant that equals 2 times Pi (2π), approximately equal to 6.283185307179586. It is useful in contexts where one complete rotation or cycle is relevant, such as in physics and engineering.



4. Infinity

Infinity is represented by math.inf in Python. It represents a concept of being unbounded or unlimited. Positive infinity is represented by math.inf, and negative infinity is represented by -math.inf.



5. Not a Number (NaN)

NaN, represented by math.nan, is used to indicate that a value is not a valid number. It is often encountered when performing invalid mathematical operations.



These constants provided by the math module are valuable in a wide range of mathematical and scientific calculations. They help maintain precision and accuracy in your Python programs.

Finding The Ceiling And The Floor Value

In math, the ceiling finds the smallest whole number greater or equal, while the floor identifies the largest whole number smaller or equal. Python simplifies this with math.ceil() for the ceiling and math.floor() for the floor.

Example 1: Ceiling Value

Output: Ceiling value of 4.346 is 5

Example 2: Floor Value

Output: Floor value of 4.346 is 4

These functions are particularly useful in scenarios where you need to work with integers and round numbers to the nearest whole value.

Finding the Factorial of the Number

The Python math module is a standard library module that provides a wide range of mathematical functions and constants. It's useful for performing various mathematical calculations.



In this illustration, we use the math.factorial() function to find the factorial of 5, which gives us the answer 120. You can swap out the 5 with any different whole number to determine the factorial of that specific number.

Finding the GCD

Finding the Greatest Common Divisor (GCD) in Python can be done using the math module's gcd() function. The GCD is the largest positive integer that divides two numbers without leaving a remainder.



The GCD of 15 and 5 is: 5

Finding the Absolute Value

Within Python's math module, you'll discover a nifty tool known as math.fabs() For Example:


You can rely on math.fabs() to ensure a number is positive, no matter its original sign.

Logarithmic and Power Functions

Logarithmic Functions:

Within Python's math module, you'll discover a set of tools designed for unraveling the secrets of numbers through logarithmic functions. These functions act like mathematical detectives, and they enable you to unveil the mysteries behind numbers. Use math.log() for natural logs, math.log2() for base-2, and math.log10() for base-10.

Example 1: Calculating the natural logarithm (base e) of 10:

import math
result = math.log(10)



Example 2: Calculating the base 10 logarithm of 1000:

import math
result = math.log10(1000)



Power Functions:

Power functions in Python's math module allow you to compute powers and exponentials. You can use math.exp() to calculate the exponential of a number and math.pow() to raise a number to a specific power.

Example 1: Calculating e raised to the power 2:

import math
result = math.exp(2)



Example 2: Calculating 3 raised to the power 4:

import math
result = math.pow(3, 4)



These functions are useful for various mathematical calculations in Python.

Finding the Power of Exp

The math module in Python includes the exp() function, which calculates the exponential value of a given number (e^x). It is used to find the value of e raised to a specified power. Here's a brief explanation and examples:


The exp() function calculates the value of e raised to the power of the given number.

In mathematical notation, exp(x) = e^x.

Math module in Python example:

1. Calculating e^4:

import math
result = math.exp(4)



2. Calculating e^(-3):

import math
result = math.exp(-3)



3. Calculating e^0:

import math
result = math.exp(0)
Output: 1.0

The exp() function is useful for various applications, including exponential growth calculations and probability distributions.

Finding the Power of a Number

To determine the potency of a number in Python, we can employ the math module and make use of the pow() function. For example:



In this illustration, we include the math module, and afterward, we employ the math.pow(x, y) method to compute 3 raised to the 4th power, resulting in 81.0. The pow() function needs two things to work: x is like the starting point, and y is the number that tells us how many times to use it.

Finding the Logarithm

Within the realm of Python programming, skilled mathematicians have designed a unique tool known as the "math module." This nifty tool proves incredibly useful when dealing with logarithms, which are essentially like hidden keys unlocking solutions to various mathematical and scientific challenges. To reveal these hidden solutions, all you need to do is utilize the mighty math.log() function.

  • The math.log(x) function computes the natural logarithm (base e) of x.
  • You have the option to figure out logarithms using different bases. Just provide a second value like this: math.log(x, base).

Remember to import the math module in Python before using these functions.

Finding the Square Root

In this instance, we bring in the math toolkit, establish a number (which happens to be 16), and subsequently employ the math.sqrt() tool to compute the square root. The outcome is showcased on the screen.

In this instance, we bring in the math toolkit, establish a number (which happens to be 16), and subsequently employ the math.sqrt() tool to compute the square root. The outcome is showcased on the screen.

Output of the code:

The square root of 16 is: 4.0

The math.sqrt() function can be used to find the square root of any non-negative number.

Trigonometric and Angular Functions

Python's math module is like a toolbox for solving math problems, especially when dealing with angles. It's like a compass guiding you through tasks like finding sine, cosine, and tangent, and it's your map for switching between degrees and radians.

1. Finding Sine, Cosine, and Tangent:

Math tools help find sine, cosine, and tangent angles in radians.

  • math.sin(x): This function provides the sine value of angle x.
  • math.cos(x): It offers the cosine value of angle x.
  • math.tan(x): This function gives you the tangent value of angle x.


2. Converting Values from Degrees to Radians and Vice Versa:

You can convert values between degrees and radians using these functions:

  • math.degrees(x): Converts radians x to degrees.
  • math.radians(x): Converts degrees x to radians.


In Python, these built-in operations simplify the process of handling angles and trigonometry. They take an angle measured in radians as input and give you the relevant trigonometric results as output. If you ever need to deal with degrees instead of radians, you can effortlessly switch between the two as required.

Special Functions

The Python math module is a toolbox full of mathematical tools and important numbers. In this discussion, we'll delve into the unique functions found within the math module functions in Python and learn how to put them to work.

1. Finding Gamma Value:

The math.gamma(x) function calculates the gamma value of the argument x.


2. Checking for Infinity or NaN:

You can employ the function math.isinf to determine whether a number is infinite and use math.isnan(x) to ascertain if a number is Not a Number (NaN).



Python's Math module simplifies complex math tasks, providing pi, Euler's number, division, ceiling/floor values, factorials, GCD, absolute values, logarithms, powers, and trigonometry functions. This enhances Python's suitability for scientific, engineering, and financial applications, making it a powerful math tool.


1. What is the math module in Python?

The Python math module is like a virtual math toolbox that's already included with Python. It helps you perform mathematical operations in your programs. You can use it for basic tasks, such as adding or subtracting numbers, and for more advanced math, like solving trigonometry problems and working with logarithms.

2. What constants does the math module provide?

The math module provides several constants, including:

  • Euler's Number (math.e)
  • Pi (math.pi)
  • Tau (math.tau)
  • Infinity (math.inf)
  • Not a Number (NaN) (math.nan)

3. What is Euler's Number, and how do you access it in Python?

Euler's Number, represented as "e," is approximately 2.71828182846. You can access it in Python using the following syntax:

import math
e_value = math.e

4. How can I calculate the factorial of a number using the math module?

You can get a number's factorial using math.factorial().

Here's an example:

import math
number = 5
factorial = math.factorial(number)

5. What is the purpose of the ceil() and floor() functions in the math module?

The "ceil" function is like a helpful elevator that takes your decimal number up to the next higher whole number. Meanwhile, the "floor" function acts as a floor beneath your decimal, finding the largest whole number just below it or equal to it. These functions come to your rescue when you need to nudge your numbers to the nearest whole figure, whether it's up or down.



Director of Engineering

Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working … Read More

Get Free Career Counselling
form image
By clicking, I accept theT&Cand
Privacy Policy
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.

upGrad Learner Support

Talk to our experts. We’re available 24/7.


Indian Nationals

1800 210 2020


Foreign Nationals



upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...