Math Module

The math module provides access to a wide range of mathematical functions and constants that go beyond Python's basic arithmetic operators - essential for any data science or scientific computing work.

Common Functions

import math

print(math.sqrt(25))       # 5.0
print(math.pow(2, 3))      # 8.0
print(math.factorial(5))   # 120
print(math.log(100, 10))   # 2.0 (log base 10 of 100)
print(math.ceil(4.2))      # 5
print(math.floor(4.8))     # 4

Trigonometric Functions

import math

print(math.sin(math.pi / 2))   # 1.0
print(math.cos(0))              # 1.0

Useful Constants

print(math.pi)   # 3.141592653589793
print(math.e)    # 2.718281828459045
While NumPy is generally preferred for mathematical operations on entire arrays of data, the math module remains the right choice for simple, single-value calculations, since it's lighter weight and doesn't require an extra installation.

Coming Up Next

Next, you'll learn the datetime module - Python's toolkit for working with dates, times, and durations.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course