Python provides the math
module for different mathematical functions. These functions can be used for calculations like ceil, factorial, floor, square root, mod, remainder, etc. The math module is a built-in module that is provided without any extra installation. The import math
can be used to import math modules and use provided functions. In this tutorial, we examine different functions provided by the math module by importing them.
“import math”
We can use the math module and provided functions by importing it with the import math
.
import math
We can also add some alias to the math module name in order to call it a short name. In the following example, we set the math module alias as m where we can use provided functions by using an alias.
import math as m
print(m.Pi)
Minimum Value
The min()
function is used to return the minimum value from the provided list of values.
import math
minimum = math.min(7,4,9,5)
print(minimum)
Maximum Value
Similar to the minimum we can use max()
function of the math module to find the maximum value.
import math
maximum = math.max(7,4,9,5)
print(maximum)
Absolute Value
The abs()
method of the math module can be used to return absolute value for the specified value.
import math
a = math.abs(-5)
b = math.abs(5)
Power
Power can be calculated with the pow()
method of the math module. The base is the first parameter and power is the second parameter.
import math
result = math.pow(4,5)
Square Root
The square root is implemented as sqrt()
in the math module.
import math
a = math.sqrt(9)
Pi Value
The Pi
value is used in different calculations related to geometry. The Pi value can be provided via the math module without any extra definition.
import math
print(math.pi)