For advanced math problems, we might have a hard time writing the code manually. Thankfully, we have the math module in python which solves complex mathematical problems for us
Built-in Functions
First, we’ll see some Built-in Math Functions, given to us in python
Max and Min
In order to find the lowest or highest value in a list, we use max and min functions.
list = [1, 2, 3, 4] | |
a = max(list) | |
b = min(list) | |
print(a, b) |
4,1
Absolute value
Absolute value can be referred the distance of the number from zero
a = -2.43 | |
print(abs(a)) |
2.43
The Math Module
For advanced mathematics, python has created a package called math.
Let’s see it in action!
Square root
Here we are importing math and using it to find the square root of 64
import math | |
a = 64 | |
print(math.sqrt(a)) |
6
Factorial
Factorial means the product of all positive numbers less than or equal to the number for which we want
to find the factorial
import math | |
print(math.factorial(3)) |
6
Trigonometry
We can also use the math module in Trignometry.
import math
a = 22 # radians
print(math.sin(a))
-0.00885
Here we are able to find the sin for an angle of 22 in radians.