Python - math Module
Mathematics is an essential part of programming. Whether you are building games, scientific applications, or data analysis tools, you often need mathematical functions.
Python provides a built-in module called math that offers a wide range of mathematical operations.
In this tutorial, you will learn how to use the Python math module with simple examples.
What is the math Module?
The math module is:
A built-in Python library that provides mathematical functions like square root, power, logarithms, trigonometry, and constants.
Importing math Module
import mathCommon Math Constants
1. Pi (π)
import math
print(math.pi)2. Euler’s Number (e)
import math
print(math.e)Basic Mathematical Functions
1. Square Root
import math
print(math.sqrt(25))2. Power Function
import math
print(math.pow(2, 3))3. Absolute Value
import math
print(math.fabs(-10))Rounding Functions
1. Ceil (Round Up)
import math
print(math.ceil(4.3))2. Floor (Round Down)
import math
print(math.floor(4.9))Factorial
import math
print(math.factorial(5))Greatest Common Divisor (GCD)
import math
print(math.gcd(12, 18))Least Common Multiple (LCM)
import math
print(math.lcm(12, 18))Trigonometric Functions
1. Sine
import math
print(math.sin(math.pi / 2))2. Cosine
import math
print(math.cos(0))3. Tangent
import math
print(math.tan(math.pi / 4))Logarithmic Functions
Natural Log (ln)
import math
print(math.log(10))Log Base 10
import math
print(math.log10(100))Degrees and Radians Conversion
Radians to Degrees
import math
print(math.degrees(math.pi))Degrees to Radians
import math
print(math.radians(180))Advanced Functions
1. Exponential Function
import math
print(math.exp(2))2. Hypotenuse Calculation
import math
print(math.hypot(3, 4))Real-World Example: Area of Circle
import math
radius = 5
area = math.pi * math.pow(radius, 2)
print("Area of Circle:", area)Real-World Example: Distance Calculation
import math
x1, y1 = 1, 2
x2, y2 = 4, 6
distance = math.hypot(x2 - x1, y2 - y1)
print("Distance:", distance)Why Use math Module?
- Provides ready-made functions
- Improves accuracy
- Saves time
- Reduces code complexity
- Useful for scientific and engineering applications
Common Use Cases
- Game development
- Data science
- Machine learning
- Physics simulations
- Financial calculations
Best Practices
1. Always import math first
import math2. Use built-in functions instead of manual calculations
3. Use constants like math.pi instead of hardcoding values
4. Keep code readable and simple
Common Mistakes
1. Forgetting to import math
# Error: name 'math' is not defined2. Using incorrect function names
3. Mixing degrees and radians
Summary
The Python math module provides powerful mathematical functions for calculations, geometry, trigonometry, and scientific computing. It helps developers perform complex math operations easily and accurately.
Key Takeaways
- Use
import mathto access functions - Includes constants like pi and e
- Provides advanced math functions
- Useful for real-world applications
- Improves accuracy and efficiency
Mastering the math module is essential for scientific programming and problem-solving in Python.


0 Comments