📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

Python Math Functions: Complete Guide with the math Module

Quick answer: Learn Python's built-in math functions and the math module, including sqrt, ceil, floor, factorial, log, trigonometric functions, and constants.

Built-In Numeric Functions (No Import Needed)

print(abs(-15))       # 15
print(round(3.456, 2)) # 3.46
print(max(4, 9, 2))    # 9
print(min(4, 9, 2))    # 2
print(sum([1, 2, 3]))  # 6
print(pow(2, 5))       # 32

The math Module

For more advanced mathematical operations, Python's standard library math module needs to be imported first.

import math

Rounding functions

print(math.ceil(4.2))   # 5, rounds up
print(math.floor(4.8))  # 4, rounds down
print(math.trunc(4.8))  # 4, simply cuts off the decimal

Square root and powers

print(math.sqrt(16))     # 4.0
print(math.pow(2, 5))    # 32.0 (always returns a float, unlike the ** operator)

Factorial

print(math.factorial(5))  # 120  (5 * 4 * 3 * 2 * 1)

Logarithms

print(math.log(100, 10))  # 2.0, log base 10 of 100
print(math.log(math.e))   # 1.0, natural log
print(math.log2(8))       # 3.0, log base 2

Trigonometric functions

print(math.sin(math.pi / 2))   # 1.0
print(math.cos(0))              # 1.0
print(math.degrees(math.pi))    # 180.0
print(math.radians(180))        # 3.14159...

Useful Constants

print(math.pi)   # 3.141592653589793
print(math.e)    # 2.718281828459045
print(math.inf)  # infinity, useful as a starting value when finding a minimum
print(math.gcd(12, 18))   # 6

math Module vs NumPy

The built-in math module works on single numbers. When you need the same operations applied across an entire array or column of data efficiently, NumPy's vectorised equivalents, such as numpy.sqrt() on a whole array at once, are the better choice.

Common Interview Questions

What is the difference between math.floor() and int()?

For positive numbers, they behave the same. For negative numbers, they differ: math.floor(-4.5) gives -5, while int(-4.5) gives -4, since int() truncates toward zero rather than rounding down.

Why use math.pow() instead of the ** operator?

math.pow() always returns a float, while ** returns an integer if both operands are integers, which can matter when a consistent type is needed downstream.

FAQ

Frequently Asked Questions

What is the difference between math.floor() and int() in Python?

For positive numbers they behave the same, but for negative numbers they differ: math.floor() always rounds down, while int() truncates toward zero.

How do you calculate a square root in Python?

Using math.sqrt(), such as math.sqrt(16), which returns 4.0.

What is the difference between the math module and NumPy for numeric operations?

The math module works on single numbers. NumPy provides vectorised equivalents that apply efficiently across entire arrays, which matters for larger datasets.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles