Learn Python – Python Math Module- Basic and advance

Python math module is defined as the most well-known mathematical functions, which includes trigonometric functions, illustration functions, logarithmic functions, etc. Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number, etc.

Pie (n): It is a popular mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its cost is 3.141592653589793.

Euler’s number(e): It is described as the base of the herbal logarithmic, and its fee is 2.718281828459045.

There are special math modules which are given below:

math.log()

This method returns the herbal logarithm of a given number. It is calculated to the base e.

Example

import math    
number = 2e-7  # small value of of x    
print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))    

Output:

log(fabs(x), base) is : -6.698970004336019

math.log10()

This approach returns base 10 logarithm of the given quantity and referred to as the fashionable logarithm.

Example

import math    
x=13  # small value of of x    
print('log10(x) is :', math.log10(x))    

Output:

log10(x) is : 1.1139433523068367 

math.exp()

This approach returns a floating-point wide variety after raising e to the given number.

Example

import math    
number = 5e-2  # small value of of x    
print('The given number (x) is :', number)    
print('e^x (using exp() function) is :', math.exp(number)-1)    

Output:

The given number (x) is : 0.05
e^x (using exp() function) is : 0.05127109637602412

math.pow(x,y)

This method returns the energy of the x corresponding to the price of y. If cost of x is poor or y is no longer integer price than it raises a ValueError.

Example

import math  
number = math.pow(10,2)  
print("The power of number:",number)  

Output:

The power of number: 100.0

math.floor(x)

This approach returns the floor cost of the x. It returns the less than or equal price to x.

Example:

import math  
number = math.floor(10.25201)  
print("The floor value is:",number)  

Output:

The floor value is: 10

math.ceil(x)

This method returns the ceil value of the x. It returns the greater than or equal cost to x.

import math  
number = math.ceil(10.25201)  
print("The floor value is:",number)  

Output:

The floor value is: 11

math.fabs(x)

This method returns the absolute value of x.

import math  
number = math.fabs(10.001)  
print("The floor absolute is:",number)  

Output:

The absolute value is: 10.001

math.factorial()

This method returns the factorial of the given number x. If x is now not integral, it raises a ValueError.

Example

import math  
number = math.factorial(7)  
print("The factorial of number:",number)  

Output:

The factorial of number: 5040

math.modf(x)

This technique returns the fractional and integer components of x. It carries the sign of x is float.

Example

import math  
number = math.modf(44.5)  
print("The modf of number:",number)  

Output:

The modf of number: (0.5, 44.0)

Python presents the countless math modules which can operate the complex challenge in single-line of code. In this tutorial, we have discussed a few essential math modules.