Class 12: Python Math Inbuilt Functions

In python number of mathematical operations can be performed with ease by importing a module named “math” which defines various functions which makes our tasks easier.

ceil()

This function returns the smallest integral value greater than the number. If number is already integer, same number is returned.

Example:1

import math
print("ceil function")
a=2.3
print(math.ceil(a))
a=12.9
print(math.ceil(a))
a=-12.23
print(math.ceil(a))
a=-62.36
print(math.ceil(a))

output:

ceil function
3
13
-12
-62
>>>

#Example:2

from math import ceil
print("ceil function")
a=2.3
print(ceil(a))
a=12.9
print(ceil(a))

output:

ceil function
3
13
>>>

#Example:3

from math import ceil as c
from math import floor as f
print("ceil function")
a=2.3
print(c(a))
a=12.9
print(c(a))

Output:

ceil function
3
13
>>>

floor()

This function returns the greatest integral value smaller than the number. If the number is already an integer, the same number is returned.

Example:

import math
print("floor function")
a=23.67
print(math.floor(a))
a=23.17
print(math.floor(a))
a=-23.77
print(math.floor(a))
a=-23.99
print(math.floor(a))

Output:

floor function
23
23
-24
-24
>>>

Example:2

from math import floor
print("floor function")
a=23.67
print(floor(a))
a=23.97
print(floor(a))

Output:

floor function
23
23
>>>

Example:3

from math import floor as f
print("floor function")
a=23.67
print(f(a))
a=23.97
print(f(a))

output:

floor function
23
23
>>>

fabs()

This function returns the absolute value of the number.

Example:

import math
print("fabs function")
a=-2.3
print(math.fabs(a))
a=-12.9
print(math.fabs(a))

Output:

fabs function
2.3
12.9
>>>

factorial()

This function returns the factorial of the number. An error message is displayed if number is not integral.

Example:

import math
print("factorial function")
a=4
print(math.factorial(a))
a=5
print(math.factorial(a))

Output:

factorial function
24
120
>>>

Python String Inbuilt Function       Python Math Inbuilt Function       Python Data and Time Inbuilt Functions       Python Random Module Functions

Number Of Visitors

0270408