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:
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 (xy).
Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
x = pow(4, 3)
print(x)
Try it Yourself »
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 math
When 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:
import math
x = math.ceil(1.4)
y = math.floor(1.4)
print(x) # returns 2
print(y) # returns 1
Try it Yourself »
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.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!