PythonMath
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
Themin()
andmax()
functions can be used to find the lowest or highest value in an iterable:
Theabs()
function returns the absolute (positive) value of the specified number:
Thepow(x,y)
function returns the value of x to the power of y (xy).
Example
Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
print(x)
The Math Module
Python has also a built-in module calledmath
, which extends the list of mathematical functions.
To use it, you must import themath
module:
When you have imported themath
module, you can start using methods and constants of the module.
Themath.sqrt()
method for example, returns the square root of a number:
Themath.ceil()
method rounds a number upwards to its nearest integer, and themath.floor()
method rounds a number downwards to its nearest integer, and returns the result:
Example
x = math.ceil(1.4)
y = math.floor(1.4)
print(x) # returns 2
print(y) # returns 1
Themath.pi
constant, returns the value of PI (3.14...):
Complete Math Module Reference
In ourMath Module Reference you will find a complete reference of all methods and constants that belongs to the Math module.