Python provides the following built-in mathematical functions. The illustration of each is shown in below:
>>> abs(15) #abs ( ) - returns the absolute value
15
>>> abs(-15)
15
>>> abs(1.5)
1.5
>>> pow(2,5) #pow ( ) – returns the value of 2 to the power of 5(here)
32
>>> pow(3,2)
9
>>> round(6.9) #round( )-returns the nearest round value of that number
7
>>> round(6.2)
6
>>> round(6.5)
6
>>> round(4.5,2)
4.5
>>> round(4.67585,2) #Here 2 represents how many values are to be printed after the point
4.68
>>> round(4.4563847,3)
4.456
>>> max(1,1.1,-1.3) #max( ) – returns the maximum value.
1.1
>>> max(16,-9,0)
16
>>> min(1,1.1,-1.3) #min( ) – returns the minimum value
-1.3
>>> min(16,-9,0)
-9
Mathematical functions in math module
Python provides some built-in mathematical functions for doing calculations. Before going to that we should know about math module.
In Python the math module is a standard module. To use mathematical functions, you have to import the module using import math.
Some standard mathematical functions in math module are described in below examples.
>>> math.pi #math.pi - returns the value of pi
3.141592653589793
>>> math.e #math.e - returns the value of e
2.718281828459045
>>> math.ceil(5.6) #math.ceil(5.6) – returns the nearest integer greater than or equal to 5.6
6
>>> math.ceil(5.3)
6
>>> math.ceil(5)
5
>>> math.floor(5.6) # math.floor(5.6) – returns the nearest integer smaller than or equal to 5.6
5
>>> math.floor(5.3)
5
>>> math.sqrt(64) #math.sqrt(64)–returns the sqrt of 64 as float 8.0
>>> math.sqrt(121)
11.0
>>> math.log(2) # math.log(2) – returns the logarithmic value of 2
0.6931471805599453