python math module
Python math Module example with result
Python math Module – Simple Examples with Results
The math module provides mathematical functions such as square root, power,
factorial, trigonometry, ceiling, floor, etc.
1. Square Root
import math
x =
25
print("Square Root:", math.sqrt(x))
Result:
Square
Root: 5.0
2. Power
import math
print("2 raised to 3:", math.pow(2,
3))
Result:
2
raised to 3: 8.0
3. Factorial
import math
print("Factorial of 5:", math.factorial(5))
Result:
Factorial
of 5: 120
4. Ceiling and Floor
import math
x =
10.7
print("Ceiling:", math.ceil(x))
print("Floor:", math.floor(x))
Result:
Ceiling:
11
Floor:
10
5. Absolute Value
import math
x =
-25
print("Absolute Value:", math.fabs(x))
Result:
Absolute
Value: 25.0
6. GCD – Greatest Common Divisor
import math
print("GCD:", math.gcd(24,
36))
Result:
GCD:
12
7. Trigonometric Functions
import math
angle =
0
print("sin(0):", math.sin(angle))
print("cos(0):", math.cos(angle))
Result:
sin(0):
0.0
cos(0):
1.0
8. Mathematical Constants
import math
print("Value of Pi:", math.pi)
print("Value of e:", math.e)
Result:
Value
of Pi: 3.141592653589793
Value
of e: 2.718281828459045
9. Logarithm
import math
print("Log of 10:", math.log(10))
Result:
Log
of 10: 2.302585092994046
10. Complete Simple Example
import math
number =
16
print("Number:", number)
print("Square Root:", math.sqrt(number))
print("Square:", math.pow(number, 2))
print("Factorial:", math.factorial(number))
print("Ceiling:", math.ceil(16.5))
print("Floor:", math.floor(16.5))
Result:
Number:
16
Square
Root: 4.0
Square:
256.0
Factorial:
20922789888000
Ceiling:
17
Floor:
16
Common math functions: sqrt(), pow(), factorial(), ceil(), floor(), fabs(), gcd(), sin(), cos(), tan(), log(), pi, and e.
Comments
Post a Comment