Math module in Python

math is a built-in module in python,it consists function like ceil(), floor() , sqrt() , pow(), pi etc.

Python ceil() function

ceil() function return ceiling value of number.

Example
import math
z=5.99
k=math.ceil(5/2)
print("Ceiling value of z:",math.ceil(z))
print("Ceiling value of k:",k)
Output
Ceiling value of z: 6
Ceiling value of k: 3

Python floor() function

floor() function return floor value of number.

Example
import math
z=5.99
k=math.floor(5/2)
print("floor value of z:",math.floor(z))
print("floor value of k:",k)

Output

floor value of z: 5
floor value of k: 2

Python sqrt() function

sqrt() function return square root of the number.

Example

import math
z=5
k=math.sqrt(7)
print("Square root of z:",math.sqrt(z))
print("Square root of k:",k)

Output

Square root of z: 2.23606797749979
Square root of k: 2.6457513110645907

Python pow() function

pow() function return power of any number.

Example

import math
z=5
p=4
k=math.pow(7,3)
print("Power of z to p:",math.pow(z,p))
print("Power of 7 to 3:",k)

Output

Power of z to p: 625.0
Power of 7 to 3: 343.0

Python pi function

pi function return value of pi.

Example

import math
k=math.pi
print("Value of Pi is:",math.pi)
print("Value of Pi is:",k)

Output

Value of Pi is: 3.141592653589793
Value of Pi is: 3.141592653589793

Some other math function

max() and min()

max() function return maximum value in a sequence and min() function return minimum value in a sequence.

Example

max1=max(22,34,55,66)
min1=min(22,34,55,66)
list1=[12,23,54,23,53]
list2=[12,23,54,23,53]
print("Maximum value max1:",max1)
print("Maximum value list1:",max(list1))
print("Minimum value min1:",min1)
print("Minimum value list2:",min(list2))

Output

Maximum value max1: 66
Maximum value list1: 54
Minimum value min1: 22
Minimum value list2: 12

Python abs() function

abs() function return positive of any number.

Example

k=abs(-4)
p=-6
print("Absolute value of k is:",k)
print("Absolute value of p is:",abs(p))

Output

Absolute value of k is: 4
Absolute value of p is: 6