Python Math Module – Complete Guide with Examples

Introduction

Python provides basic arithmetic operators like +, -, *, /, and %, it also includes a powerful built-in module called the Math Module that offers advanced mathematical functions and constants.

The Python Math Module contains functions for:

  • Square roots
  • Powers and exponents
  • Logarithms
  • Trigonometric calculations
  • Factorials
  • Rounding operations
  • Mathematical constants

Note: Using the Math Module makes mathematical computations easier, faster, and more accurate.

What is the Python Math Module?

The Math Module is a built-in Python module that provides access to various mathematical functions and constants.

It is especially useful for performing advanced mathematical calculations that go beyond basic arithmetic operations.

Note: Since it is part of Python’s standard library, no installation is required.

Importing the Math Module

Before using the Math Module, you must import it.

Syntax:


import math

Example:


import math
print(math.sqrt(25))

Output:

5.0

Mathematical Constants

The Math Module provides useful mathematical constants.

1. math.pi

Represents the value of π (pi).

Example:


import math
print(math.pi)

Output:

3.141592653589793

2. math.e

Represents Euler’s number.

Example:


import math
print(math.e)

Output:

2.718281828459045

3. math.tau

Represents 2π.

Example:


import math
print(math.tau)

Output:

6.283185307179586

4. math.inf

Represents positive infinity.

Example:


import math
print(math.inf)

Output:

inf

Square Root Function

The sqrt() function calculates the square root.

Syntax:


math.sqrt(number)

Example:


import math
print(math.sqrt(64))

Output:

8.0

Power Function

The pow() function calculates powers.

Syntax:


math.pow(x, y)

Example:


import math
print(math.pow(2, 3))

Output:

8.0

Equivalent to:


2 ** 3

Absolute Value

The fabs() function returns the absolute value.

Example:


import math
print(math.fabs(-25))

Output:

25.0

Ceiling Function

The ceil() function rounds up to the nearest integer.

Example:


import math
print(math.ceil(5.2))

Output:

6

Floor Function

The floor() function rounds down to the nearest integer.

Example:


import math
print(math.floor(5.9))

Output:

5

Truncation Function

The trunc() function removes the decimal part.

Example:


import math
print(math.trunc(5.99))

Output:

5

Factorial Function

A factorial is the product of all positive integers up to a number.

Syntax:


math.factorial(n)

Example:


import math
print(math.factorial(5))

Output:

120

Calculation:


5 × 4 × 3 × 2 × 1 = 120

Greatest Common Divisor (GCD)

The gcd() function finds the greatest common divisor.

Example:


import math
print(math.gcd(24, 36))

Output:

12

Logarithmic Functions

Natural Logarithm

The log() function calculates the natural logarithm.

Example:


import math
print(math.log(10))

Output:

2.302585092994046

Log Base 10

Example:


import math
print(math.log10(100))

Output:

2.0

because 10² = 100.

Log Base 2

Example:


import math
print(math.log2(8))

Output:

3.0

because 2³ = 8.

Exponential Function

The exp() function calculates e raised to a power.

Example:


import math
print(math.exp(2))

Output:

7.38905609893065

equivalent to .

Trigonometric Functions

The Math Module includes trigonometric functions.

Sine

Example:


import math
print(math.sin(math.radians(90)))

Output:

1.0

Cosine

Example:


import math
print(math.cos(math.radians(0)))

Output:

1.0

Tangent

Example:


import math
print(math.tan(math.radians(45)))

Output:

0.9999999999999999

Approximately equal to 1.

Converting Degrees and Radians

Degrees to Radians

Example:


import math
print(math.radians(180))

Output:

3.141592653589793

Radians to Degrees

Example:


import math
print(math.degrees(math.pi))

Output:


180.0

Finding Maximum and Minimum

Although Python provides built-in functions, they are commonly used with mathematical operations.

Example:


numbers = [10, 20, 30, 5]
print(max(numbers))
print(min(numbers))

Output:

30
5

Checking Special Numbers

isfinite()

Checks if a number is finite.

Example:


import math
print(math.isfinite(100))

Output:

True

isnan()

Checks if a value is NaN.

Example:


import math
print(math.isnan(float('nan')))

Output:

True

isinf()

Checks if a value is infinite.

Example:


import math
print(math.isinf(math.inf))

Output:

True

Real-Life Example: Circle Area Calculator


import math
radius = 5
area = math.pi * radius ** 2
print(area)

Output:

78.53981633974483

Used in geometry applications.

Real-Life Example: Loan Interest Calculation


import math
principal = 10000
rate = 5
time = 2
amount = principal * math.pow(
    (1 + rate/100),
    time
)
print(amount)

Output:

11025.0

Useful in banking software.

Real-Life Example: Distance Formula


import math
x1, y1 = 2, 3
x2, y2 = 8, 7
distance = math.sqrt(
    (x2 - x1)**2 +
    (y2 - y1)**2
)
print(distance)

Output:

7.211102550927978

Used in GIS and gaming applications.

Real-Life Example: Factorial Calculator


import math
number = 6
print(
    math.factorial(number)
)

Output:

720

Useful in probability and statistics.

Commonly Used Math Functions

Code Description
sqrt() Square root
pow() Power calculation
fabs() Absolute value
ceil() Round up
floor() Round down
trunc() Remove decimals
factorial() Factorial
gcd() Greatest common divisor
log() Natural logarithm
exp() Exponential value
sin() Sine
cos() Cosine
tan() Tangent

Advantages of Python Math Module

Advantage Description
Built-in No installation required
Accurate Reliable calculations
Fast Optimized mathematical functions
Easy to Learn Simple syntax
Widely Used Useful in many applications

Common Mistakes

1. Forgetting to Import math

Incorrect:


print(math.sqrt(25))

Output:

NameError

Correct:


import math

2. Using Negative Values with sqrt()

Incorrect:


math.sqrt(-4)

Output:

ValueError

For complex numbers, use the cmath module.

3. Confusing floor() and trunc()

Example:


math.floor(-5.8)

Output:

-6

Example:


math.trunc(-5.8)

Output:

-5

4. Forgetting Radians in Trigonometric Functions

Incorrect:


math.sin(90)

Correct:


math.sin(
    math.radians(90)
)

5. Using factorial() with Decimal Values

Incorrect:


math.factorial(5.5)

Output:

TypeError

Factorial accepts only integers.

Best Practices

1. Import Only When Needed


import math

at the beginning of your program.

2. Use Meaningful Variable Names

Good:


radius
area
distance

3. Validate Input

Check for invalid values before mathematical calculations.

4. Use Built-in Functions

Avoid writing custom mathematical logic when a built-in function exists.

5. Understand Function Limitations

Some functions accept only specific types of values.

Conclusion

The Python Math Module is a powerful built-in library that provides a wide range of mathematical functions and constants for performing advanced calculations.

It is widely used in scientific computing, finance, engineering, gaming, data analysis, and machine learning applications.

Related Python Tutorials