Introduction to Python

Mathematical Functions in Python

Lesson 5: Functions in Python

The Math Module

Python comes with a built-in math module that provides a wide range of mathematical functions. To use it, you first need to import it at the top of your script.

import math

A toolbox labeled math module with mathematical symbols like square root, pi, and factorial spilling out of it

Common Math Functions

Python Math Module Functions

FunctionDescriptionExampleResult
math.sqrt(x)Square root of xmath.sqrt(25)5.0
math.pow(x, y)x raised to the power ymath.pow(2, 3)8.0
math.floor(x)Rounds down to nearest integermath.floor(3.9)3
math.ceil(x)Rounds up to nearest integermath.ceil(3.1)4
abs(x)Absolute value of xabs(-7)7
math.piThe value of πmath.pi3.14159...
math.log(x)Natural logarithm of xmath.log(1)0.0
math.factorial(x)Factorial of xmath.factorial(5)120

Visualizing Math Functions

A square with side 5 and area 25 labeled to illustrate square root — sqrt(25) = 5
sqrt() — Square Root
A number line showing 3.7 between 3 and 4 with floor pointing to 3 and ceil pointing to 4
floor() and ceil()
A circle with radius labeled r and the pi symbol showing the circumference formula
math.pi — π
The calculation 5 factorial = 5 x 4 x 3 x 2 x 1 = 120 written out step by step
factorial()

Using Math Functions

import math # Square root print(math.sqrt(144)) # 12.0 # Power print(math.pow(3, 4)) # 81.0 # Rounding print(math.floor(7.8)) # 7 print(math.ceil(7.2)) # 8 # Circle area radius = 5 area = math.pi * math.pow(radius, 2) print(round(area, 2)) # 78.54

Remember to import math before using any math module functions. You can also import specific functions using: from math import sqrt, pi

Subscribe to our newsletter.

Get updates to news and events.