Nearby lessons
69 of 108Python - Math Module
Introduction
Python provides a built-in module called math for performing mathematical calculations.
The math module contains many ready-to-use mathematical functions and constants.
Why Math Module is Required?
Sometimes programs need complex mathematical calculations.
Writing these calculations manually can be difficult.
The math module provides ready-made functions to make calculations easier.
Importing the Math Module
Before using the math module, you must import it.
Syntax:
import math
Example - Import Math Module
main.py
5.0
Features of Math Module
The math module provides:
- Mathematical constants
- Mathematical functions
- Trigonometric functions
- Logarithmic functions
Mathematical Constants
The math module provides useful mathematical constants.
Value of PI
Use math.pi to get the value of PI.
Example - math.pi
main.py
3.141592653589793
Value of Euler Number
Use math.e to get the value of Euler's number.
Example - math.e
main.py
2.718281828459045
Square Root Function
The math.sqrt() function returns the square root of a number.
Example - Square Root
main.py
8.0
Power Function
The math.pow() function calculates the power of a number.
Example - Power
main.py
8.0
Absolute Value
The math.fabs() function returns the absolute (positive) value of a number.
Example - Absolute Value
main.py
25.0
Ceiling Function
The math.ceil() function returns the nearest greater integer.
Example - Ceiling Function
main.py
11
Floor Function
The math.floor() function returns the nearest smaller integer.
Example - Floor Function
main.py
10
Factorial Function
The math.factorial() function returns the factorial of a positive integer.
Example - Factorial
main.py
120
GCD Function
The math.gcd() function returns the Greatest Common Divisor (GCD) of two numbers.
Example - GCD
main.py
6
Trigonometric Functions
The math module provides trigonometric functions such as sin(), cos(), and tan().
Example - sin()
main.py
0.0
Example - cos()
main.py
1.0
Example - tan()
main.py
0.0
Logarithmic Functions
The math module provides logarithmic functions.
Natural Logarithm
Use math.log() to calculate the natural logarithm.
Example - math.log()
main.py
2.302585092994046
Base 10 Logarithm
Use math.log10() to calculate the base-10 logarithm.
Example - math.log10()
main.py
2.0
Angle Conversion
The math module can convert angles between degrees and radians.
Degrees to Radians
main.py
3.141592653589793
Radians to Degrees
main.py
180.0
Maximum and Minimum Values
Python provides built-in functions max() and min() to find the largest and smallest values.
Example - max() and min()
main.py
50 10
Sum Function
The built-in sum() function returns the total of all values in a sequence.
Example - sum()
main.py
60
Random Mathematical Operation
main.py
5.477225575051661
Difference Between Built-in Functions and math Module
| Built-in Functions | math Module |
|---|---|
| Available directly. | Must be imported before use. |
Example: abs() |
Example: math.sqrt() |
Real World Usage
The math module is commonly used in:
- Scientific calculations
- Engineering software
- Data analysis
- Artificial Intelligence (AI)
- Machine Learning (ML)
- Financial applications
Example Areas
| Usage | Example |
|---|---|
| Geometry | Area Calculation |
| Banking | Interest Calculation |
| AI | Mathematical Formulas |
Important Notes
- The math module provides advanced mathematical functions.
- You must import the module before using it.
- It supports trigonometric and logarithmic operations.
- It is widely used for scientific and engineering calculations.
Quick Summary
| Function | Purpose |
|---|---|
sqrt() | Square Root |
pow() | Power Calculation |
factorial() | Factorial |
ceil() | Round Up |
floor() | Round Down |
gcd() | Greatest Common Divisor |