# Python Math Module

The math module is one of the most popular and commonly used modules in Python. As the name suggests, the math module contains pre-built mathematical operations.

Using it, we can import several useful functions that can help in performing mathematical operations. Moreover, the math module also contains mathematical constants.

The following are some of the uses of Python math module functions. (full list)

• Calculating factorials.
• Rounding off a float number.
• Solving quadric and trigonometric equations like sin, cos, tan, asin etc.
• Calculating the base to the exponent power.
• Calculating the square root of a number.
• calculating log
• calculating permutation and combination

## Constants in Math Module

The math module also comes with some constants namely:

## Import math

To use the functions and constants of math module we first import the built-in math module:

``import math``

Instead of importing the full module we can import cherry picked function by:

``from math import <function-name>``

## Functions in Math Module

Functions in math module are categorized based on there mathematical usage.

## Example of some commonly used math module fuctions

### math.factorial()

The math.factorial() function is used to get the factorial of a number. Only positive integers are accepted by this function.

``````import math

a = math.factorial(5)
b = math.factorial(10)
c = math.factorial(20)

print(a)
print(b)
print(c)
``````

## math.pow

The math.pow() function is to find the value of x raised to power y. It always returns a float value

``````import math

a = math.pow(2,2)
b = math.pow(5,7)
c = math.pow(7,9)

print(a)
print(b)
print(c)
``````

## math.ceil and math.floor

The math.ceil() function returns the smallest integer greater than or equal to the number.

math.floor() function returns the largest integer less than or equal to the number.

## math.sqrt

The math.sqrt() function is used to find the square root of a number.

``````import math

a = math.sqrt(5)
b = math.sqrt(20)
c = math.sqrt(35)

print(a)
print(b)
print(c)
``````

## math.fsum

The math.fsum() function is used to find the sum of all the items of an iterable such as list, tuple, and set. It always returns a float value.

``````import math

a = math.fsum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
b = math.fsum((10, 20, 30, 40, 50))
c = math.fsum({1.1, 2.2, 3.3, 4.4, 5.5})

print(a)
print(b)
print(c)
``````

Note: The functions in the math module do not accept complex values. To use complex values use cmath module instead.