Python math Module Overview
The math module is a built-in Python module for common math work.
It gives you extra functions and constants that are not available as basic language features. Beginners often use it for square roots, rounding, powers, and simple numeric calculations.
This page is an overview. It shows what the module is, how to import it, and which parts are most useful to start with.
Quick example
import math
print(math.sqrt(25))
print(math.ceil(3.2))
print(math.floor(3.8))
print(math.pi)
Expected output:
5.0
4
3
3.141592653589793
Use import math first. Then call functions and constants with the math. prefix.
What the math module is
The math module is part of Python’s standard library.
That means:
- You do not need to install it
- It is included with Python
- You can use it after importing it
The module gives you:
- Extra math functions
- Useful numeric constants
- A simple way to do common calculations
You will often use it for:
- Square roots
- Rounding up or down
- Powers
- Trigonometry
- Factorials
How to import the math module
Import the module at the top of your file:
import math
After that, use dot notation to access items inside the module:
import math
print(math.sqrt(9))
Output:
3.0
The important idea is:
mathis the module namesqrtis a function inside that modulemath.sqrt(9)means “use thesqrtfunction from themathmodule”
Common functions beginners use
Here are some of the most common math functions for beginners.
math.sqrt(x)
Use math.sqrt(x) to find the square root of a number.
import math
print(math.sqrt(16))
Output:
4.0
For a full guide, see math.sqrt() explained.
math.ceil(x)
Use math.ceil(x) to round a number up to the next whole number.
import math
print(math.ceil(3.2))
print(math.ceil(7.0))
Output:
4
7
math.floor(x)
Use math.floor(x) to round a number down to the lower whole number.
import math
print(math.floor(3.8))
print(math.floor(7.0))
Output:
3
7
For more examples, see math.ceil() and math.floor() explained.
math.pow(x, y)
Use math.pow(x, y) to raise a number to a power.
import math
print(math.pow(2, 3))
Output:
8.0
If you want to compare it with the ** operator, see math.pow() explained.
math.fabs(x)
Use math.fabs(x) to get the absolute value as a float.
import math
print(math.fabs(-12))
Output:
12.0
If you want the normal built-in absolute value function, see Python abs() explained.
math.factorial(n)
Use math.factorial(n) to calculate a factorial.
A factorial means multiplying a whole number by all smaller positive whole numbers down to 1.
import math
print(math.factorial(5))
Output:
120
Useful constants
The math module also includes constants.
Constants are values stored in the module that do not change.
math.pi
Use math.pi when you need the value of pi.
import math
print(math.pi)
math.e
Use math.e for Euler’s number.
import math
print(math.e)
These constants are useful because they give you accurate built-in values instead of making you type them yourself.
When to use math instead of built-in functions
Python already has some built-in numeric tools, so you do not always need the math module.
Built-in functions are often enough for simple tasks, such as:
abs()round()min()max()
Use the math module when:
- You need square roots
- You need special rounding behavior like
ceil()orfloor() - You need constants like
pi - You need functions that are not built into Python directly
For example:
- Use
math.sqrt(25)for square roots - Use
round()for normal rounding - Use
abs()for a simple absolute value - Use
mathwhen you need more specialized math tools
Simple example
This example shows:
- Importing the module
- Using one function
- Using one constant
import math
radius = 3
area = math.pi * math.pow(radius, 2)
print("Radius:", radius)
print("Area:", area)
Expected output:
Radius: 3
Area: 28.274333882308138
What this code does:
import mathmakes the module availablemath.pigives the value of pimath.pow(radius, 2)squares the radius- The result is used to calculate the area of a circle
Common errors and beginner problems
Here are some common problems when using the math module.
Forgetting to import math
This causes a NameError because Python does not know what math is.
print(math.sqrt(16))
Fix:
import math
print(math.sqrt(16))
If this happens, read NameError in Python: causes and fixes.
Using sqrt() without math.
This also causes a NameError if you imported the module with import math.
import math
print(sqrt(16))
Fix:
import math
print(math.sqrt(16))
Passing invalid values
Some functions only accept certain values.
For example, math.sqrt() does not accept negative numbers in normal real-number math.
import math
print(math.sqrt(-1))
This raises a ValueError.
If you see this problem, read ValueError: math domain error fix.
Shadowing the name math
Do not use math as your own variable name.
This can overwrite the module name in your code.
import math
math = 10
print(math.sqrt(16))
This fails because math is now an integer, not the module.
What to learn next
After this overview, the best next step is to learn one function at a time.
Good next topics include:
math.sqrt()for square rootsmath.ceil()andmath.floor()for roundingmath.pow()for exponent calculations- Built-in numeric functions like
abs()andround() - Error pages if your code raises
NameErrororValueError
FAQ
Do I need to install the math module?
No. The math module comes with Python, so you only need to import it.
Why does Python say math is not defined?
You probably forgot to import math, or you used the name before importing it.
What is the difference between math.pow() and **?
Both do exponent calculations, but ** is an operator and math.pow() is a function that returns a float.
Can I use math.sqrt() on negative numbers?
No for normal real-number math. It raises a ValueError. Complex numbers use different tools.