Python math Module
🧮 Python math Module
Python math Module is a built-in module that deals with all the mathematical functions and calculations. The math
module in Python provides access to mathematical functions defined by the C standard. It includes functions for:
- Arithmetic operations
- Trigonometry
- Logarithms
- Constants like π and e
- Rounding and power functions
To use the module, you must first import it.You can import the module into your program by using the following line:
import math
math Functions
Some of the functions in the math module are as follows:
- math.sqrt(256)
- math.pi
- math.sin(math/pi/3)
- math.factorial(9)
- math.floor(12.79)
- math.pow(3,4)
Common math
Functions
Function | Description | Example | Output |
---|---|---|---|
math.sqrt(x) |
Square root of x | math.sqrt(16) |
4.0 |
math.pow(x, y) |
x raised to the power y | math.pow(2, 3) |
8.0 |
math.floor(x) |
Floor (largest integer ≤ x) | math.floor(3.9) |
3 |
math.ceil(x) |
Ceiling (smallest integer ≥ x) | math.ceil(3.2) |
4 |
math.fabs(x) |
Absolute value | math.fabs(-7.25) |
7.25 |
math.factorial(x) |
Factorial of x | math.factorial(5) |
120 |
math.log(x) |
Natural log (base e) | math.log(10) |
2.3025… |
math.log10(x) |
Logarithm base 10 | math.log10(100) |
2.0 |
math.sin(x) |
Sine (x in radians) | math.sin(math.pi/2) |
1.0 |
math.cos(x) |
Cosine of x | math.cos(0) |
1.0 |
math.tan(x) |
Tangent of x | math.tan(math.pi/4) |
1.0 |
Example Program
import math
radius = 5
area = math.pi * math.pow(radius, 2)
print("Area of circle:", area)