MyInternships.in

Python Modules & Packages

Python math Module

The math module is Python's built-in toolbox of mathematical functions and constants, going far beyond the basic + - * / operators.


Importing math

Since math ships with the standard library, no installation is needed — just import it at the top of your file to unlock dozens of mathematical functions and constants.

Getting started
Python
import math

Square Roots and Powers

math.sqrt() calculates a square root, while math.pow() raises a number to a given power. Note that math.pow() always returns a float, unlike the ** operator, which keeps integers as integers.

sqrt and pow
Python
print(math.sqrt(64))
print(math.pow(2, 5))

Rounding Down and Up

math.floor() rounds a number down to the nearest whole number, and math.ceil() rounds it up — both are more precise than round(), which rounds to the nearest value.

floor and ceil
Python
print(math.floor(4.7))
print(math.ceil(4.2))

Useful Constants

The module also stores well-known mathematical constants so you never have to type long decimal approximations yourself.

Constants
Python
print(math.pi)
print(math.e)

Factorial

math.factorial() multiplies a number by every positive whole number below it, commonly used in probability and combinatorics problems.

Factorial
Python
print(math.factorial(5))
Function / ConstantPurposeExample
math.sqrt(x)Square rootmath.sqrt(9) → 3.0
math.pow(x, y)x raised to power ymath.pow(2, 3) → 8.0
math.floor(x)Round downmath.floor(4.9) → 4
math.ceil(x)Round upmath.ceil(4.1) → 5
math.piValue of pi3.14159...
math.eEuler's number2.71828...
math.factorial(x)x!math.factorial(4) → 24
💡

For most everyday exponent math you can also use the ** operator, like 2 ** 5, which keeps integer results as integers instead of floats.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships