17 Aug Python math module
The math module in Python is used to perform mathematical tasks, with functions, including, trigonometric, logarithmic, etc.
How to import the math module
To import the math module and use its mathematical functions, write the following at the start of the Python program:
1 2 3 |
import math |
Examples – math module functions
Let us see the following examples of the math module:
- sqrt()
- ceil()
- floor()
- pow()
- fabs()
- factorial()
- sin()
- cos()
- tan()
- log()
sqrt() function in Python
To return the square root of a number, use the sqrt() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 10 |
# sqrt() function in Python # Code by studyopedia import math as m print(m.sqrt(64)) print(m.sqrt(121)) print(m.sqrt(900)) |
The output is as follows:
1 2 3 4 5 |
8.0 11.0 30.0 |
ceil() function in Python
To round a number up to the nearest integer, use the ceil() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 10 |
# ceil() function in Python # Code by studyopedia import math as m print(m.ceil(-5.35)) print(m.ceil(5.35)) print(m.ceil(96.95)) |
The output is as follows:
1 2 3 4 5 |
-5 6 97 |
floor() function in Python
To round a number down to the nearest integer number, use the floor() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 10 |
# floor() function in Python # Code by studyopedia import math as m print(m.floor(-25.98)) print(m.floor(25.98)) print(m.floor(70.15)) |
The output is as follows:
1 2 3 4 5 |
-26 25 70 |
pow() function in Python
To return the value of p to the power of q, use the pow() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# pow() function in Python # Code by studyopedia import math as m print(m.pow(2,7)) print(m.pow(4,3)) |
The output is as follows:
1 2 3 4 |
128.0 64.0 |
fabs() function in Python
To return the absolute value of a number, use the fabs() function in Python. The value returned is a float. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# fabs() function in Python # Code by studyopedia import math as m print(m.fabs(-45.89)) print(m.fabs(-98)) |
The output is as follows:
1 2 3 4 |
45.89 98.0 |
factorial() function in Python
To return the factorial of a number, use the factorial() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 |
# factorial() function in Python # Code by studyopedia import math as m print(m.factorial(7)) print(m.factorial(0)) print(m.factorial(1)) print(m.factorial(3)) |
The output is as follows:
1 2 3 4 5 6 |
5040 1 1 6 |
sin() function in Python
To return the sin of a number, use the sin() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# sin() function in Python # Code by studyopedia import math as m print(m.sin(10)) print(m.sin(m.radians(30))) |
The output is as follows:
1 2 3 4 |
-0.5440211108893698 0.49999999999999994 |
cos() function in Python
To return the cosine of a number, use the cos() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# cos() function in Python # Code by studyopedia import math as m print(m.cos(10)) print(m.cos(m.radians(30))) |
The output is as follows:
1 2 3 4 |
-0.8390715290764524 0.8660254037844387 |
tan() function in Python
To return the tangent of a number, use the tan() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# tan() function in Python # Code by studyopedia import math as m print(m.tan(45)) print(m.tan(m.radians(45))) |
The output is as follows:
1 2 3 4 |
1.6197751905438615 0.9999999999999999 |
log() function in Python
To return the logarithm of a number, use the log() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# log() function in Python # Code by studyopedia import math as m print(m.log(2)) print(m.log(1)) |
The output is as follows:
1 2 3 4 |
0.6931471805599453 0.0 |
Python Tutorial (English)
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments