Nearby lessons
76 of 159Python - Math Module
- Import the math module and use its constants math.pi and math.e
- Compute square roots, powers, absolute values, ceiling, floor, factorial, and GCD
- Use trigonometric functions such as sin(), cos(), and tan()
- Work with logarithmic functions like log() and log10()
- Convert angles between degrees and radians with radians() and degrees()
- Apply the built-in max(), min(), and sum() functions to sequences
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
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
Value of Euler Number
Use math.e to get the value of Euler's number.
Example - math.e
Square Root Function
The math.sqrt() function returns the square root of a number.
Example - Square Root
Power Function
The math.pow() function calculates the power of a number.
Example - Power
Absolute Value
The math.fabs() function returns the absolute (positive) value of a number.
Example - Absolute Value
Ceiling Function
The math.ceil() function returns the nearest greater integer.
Example - Ceiling Function
Floor Function
The math.floor() function returns the nearest smaller integer.
Example - Floor Function
Factorial Function
The math.factorial() function returns the factorial of a positive integer.
Example - Factorial
GCD Function
The math.gcd() function returns the Greatest Common Divisor (GCD) of two numbers.
Example - GCD
Trigonometric Functions
The math module provides trigonometric functions such as sin(), cos(), and tan().
Example - sin()
Example - cos()
Example - tan()
Logarithmic Functions
The math module provides logarithmic functions.
Natural Logarithm
Use math.log() to calculate the natural logarithm.
Example - math.log()
Base 10 Logarithm
Use math.log10() to calculate the base-10 logarithm.
Example - math.log10()
Angle Conversion
The math module can convert angles between degrees and radians.
Degrees to Radians
Radians to Degrees
Maximum and Minimum Values
Python provides built-in functions max() and min() to find the largest and smallest values.
Example - max() and min()
Sum Function
The built-in sum() function returns the total of all values in a sequence.
Example - sum()
Random Mathematical Operation
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 |
- The math module must be imported before its functions can be used
- math.pi and math.e provide the values of PI and the Euler number
- math.sqrt() and math.pow() return floating-point results
- math.ceil() rounds up and math.floor() rounds down
- math.gcd() finds the Greatest Common Divisor of two numbers
- The math module provides ready-made functions for scientific and engineering calculations