Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers.
Built-in Math Functions
max() functions can be used to find the lowest or highest value in an iterable:
y = max(5, 10, 25)
abs() function returns the absolute (positive) value of the specified number:
pow(x, y) function returns the value of x to the power of y (xy).
Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
Learn to Filter Data in Python Like a Data Analyst
Try a hands-on training sessions with step-by-step guidance from an expert. Try the guided project made in collaboration with Coursera now!Get started
The Math Module
Python has also a built-in module called
math, which extends the list of mathematical functions.
To use it, you must import the
When you have imported the
math module, you
can start using methods and constants of the module.
math.sqrt() method for example, returns the square root of a number:
x = math.sqrt(64)
math.ceil() method rounds a number upwards to
its nearest integer, and the
method rounds a number downwards to its nearest integer, and returns the result:
x = math.ceil(1.4)
y = math.floor(1.4)
print(x) # returns 2
print(y) # returns 1
math.pi constant, returns the value of
x = math.pi
Complete Math Module Reference
In our Math Module Reference you will find a complete reference of all methods and constants that belongs to the Math module.