Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers.
The min() and max() functions can be used to find the lowest or highest value in an iterable:
x = min(5, 10, 25)
y = max(5, 10, 25)
The abs() function returns the absolute (positive) value of the specified number:
The pow(x, y) function returns the value of x to the power of y (x y ).
Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
Python has also a built-in module called math , which extends the list of mathematical functions.
To use it, you must import the math module:
import mathWhen you have imported the math module, you can start using methods and constants of the module.
The math.sqrt() method for example, returns the square root of a number:
The math.ceil() method rounds a number upwards to its nearest integer, and the math.floor() 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
The math.pi constant, returns the value of PI (3.14. ):
In our Math Module Reference you will find a complete reference of all methods and constants that belongs to the Math module.
W3schools Pathfinder Track your progress - it's free!
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com
If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.