Python
[Basic Grammar] Python Math
grace21110
2023. 11. 24. 09:02
반응형
- Built-In Math Functions
The min() and max() functions can be used to find the lowest or highest value.
Example
x = min (3,10, 1)
y = max (3,10, 1)
print(x)
print(y)
The abs() function returns the absolute (positive value) of the specified number.
Example
x = abs(-8.25)
print(x)
The pow(x,y) function returns the value of x to the power of y.
Example
Return the value of 4 to the power of 2.
x = pow(4, 2)
print(x)
- The Math Module
The module called math extends the list of math functions.
Example
import math
x = math.sqrt(3)
print(x)
The math.ceil() rounds a number upwards to its nearest integer and math.floor() rounds a number downwards to its nearest integer.
Example
import math
x = math.ceil(1.2)
y = math.floor(1.2)
print(x) # returns 2
print(y) # returns 1
The math.pi constant, returns the value of PI (3.14...):
Example
import math
x = math.pi
print(x)