Python math Module: A Complete Guide with Examples

Last updated 5 months, 1 week ago | 369 views 75     5

Tags:- Python

Python is not just a general-purpose programming language—it’s also a powerful tool for mathematical computation. The built-in math module provides a range of useful mathematical functions and constants for both basic and advanced calculations.

In this tutorial, we’ll cover:

  • What the math module is

  • Basic math operations

  • Trigonometric functions

  • Logarithms and exponential functions

  • Special constants

  • Advanced functions (factorials, power, gcd)

  • Real-world examples

  • Tips and common pitfalls


What is the math Module?

The math module is a standard Python library that provides mathematical functions defined by the C standard. To use it, you first import it:

import math

Basic Mathematical Functions

1. math.sqrt(x) – Square root

print(math.sqrt(16))  # Output: 4.0

2. math.pow(x, y) – Power (x^y)

print(math.pow(2, 3))  # Output: 8.0

3. math.floor(x) – Rounds down

print(math.floor(3.7))  # Output: 3

4. math.ceil(x) – Rounds up

print(math.ceil(3.2))  # Output: 4

5. math.fabs(x) – Absolute value (always float)

print(math.fabs(-7))  # Output: 7.0

Trigonometric Functions

All angles are in radians, not degrees.

1. math.sin(x) – Sine

print(math.sin(math.pi / 2))  # Output: 1.0

2. math.cos(x) – Cosine

print(math.cos(0))  # Output: 1.0

3. math.tan(x) – Tangent

print(math.tan(math.pi / 4))  # Output: ~1.0

Convert between radians and degrees:

print(math.degrees(math.pi))  # Output: 180.0
print(math.radians(180))      # Output: 3.1415926535...

Logarithmic and Exponential Functions

1. math.log(x, base) – Logarithm

print(math.log(100, 10))  # Output: 2.0

2. math.log10(x) – Base 10 log

print(math.log10(1000))  # Output: 3.0

3. math.exp(x) – e^x

print(math.exp(2))  # Output: 7.389056...

Useful Constants

Constant Value
math.pi 3.141592653...
math.e 2.718281828...
math.tau 6.283185307...
math.inf Infinity
math.nan Not a Number
print(math.pi)
print(math.e)

Advanced Mathematical Functions

1. math.factorial(x)

print(math.factorial(5))  # Output: 120

2. math.gcd(x, y) – Greatest Common Divisor

print(math.gcd(48, 18))  # Output: 6

3. math.isclose(a, b, rel_tol=1e-09) – Compare floats safely

print(math.isclose(0.1 + 0.2, 0.3))  # Output: True

4. math.isfinite(x) – Checks if not infinity or NaN

print(math.isfinite(5))      # True
print(math.isfinite(math.inf))  # False

Real-World Example: Area and Circumference of a Circle

import math

def circle_properties(radius):
    area = math.pi * math.pow(radius, 2)
    circumference = 2 * math.pi * radius
    return area, circumference

r = 5
a, c = circle_properties(r)
print(f"Area: {a:.2f}, Circumference: {c:.2f}")

Output:

Area: 78.54, Circumference: 31.42

Tips for Using math

  • ✅ Use math.pow() for floating-point exponents, but ** is faster for integers.

  • ✅ Use math.isclose() instead of == for comparing floating-point numbers.

  • ✅ Remember that trigonometric functions use radians, not degrees.

  • ✅ Use math.inf and math.nan to represent infinite and undefined values in computations.


⚠️ Common Pitfalls

Pitfall Why It’s a Problem Fix
❌ Using == to compare floats May fail due to precision Use math.isclose()
❌ Passing negative numbers to math.sqrt() Causes ValueError Use cmath for complex results
❌ Confusing degrees/radians in trig functions Leads to wrong outputs Use math.radians() / math.degrees()
❌ Using math for statistical functions math doesn't support mean/median Use the statistics module

Summary Table

Function Description
math.sqrt(x) Square root
math.floor(x) Round down
math.ceil(x) Round up
math.pow(x, y) Exponentiation
math.factorial(x) Factorial
math.gcd(x, y) Greatest Common Divisor
math.sin(x) Sine (in radians)
math.log(x, base) Logarithm
math.exp(x) e^x
math.pi, math.e Constants

Conclusion

The math module is a powerful, fast, and easy-to-use library for performing numerical calculations. Whether you’re doing basic arithmetic, complex scientific computation, or geometric math, the math module is an essential part of your Python toolbox.

Explore it, use it, and combine it with other modules like random, statistics, and decimal for even more powerful capabilities.