Math Module

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

Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its value is 3.141592653589793.

Euler's number(e): It is defined as the base of the natural logarithmic, and its value is 2.718281828459045.

There are different math modules which are given below:

math.log()

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

Example
snippet
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 method returns base 10 logarithm of the given number and called the standard logarithm.

Example
snippet
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 method returns a floating-point number after raising e to the given number.

Example
snippet
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.sqrt()

This function returns the square root of any given number.

Example
snippet
import math
x = 20
y = 14
z = 17.8995   
print('sqrt of 20 is ', math.sqrt(x))
print('sqrt of 14 is ', math.sqrt(y))
print('sqrt of 17.8995 is ', math.sqrt(z))
Output
sqrt of 20 is 4.47213595499958 sqrt of 14 is 3.7416573867739413 sqrt of 17.8995 is 4.230780069916185

math.expm1()

This method returns e raised to the power of any number minus 1. e is the base of natural logarithm.

Example
snippet
import math
number = 2e-1  # small value of of x
print('The given number (x) is :', number)
print('e^x (using expml() function) is :', math.expm1(number))
Output
The given number (x) is : 0.2 e^x (using expml() function) is : 0.22140275816016985

math.cos()

It returns the cosine of any number, in radians.

Example
snippet
import math
angleInDegree = 60
angleInRadian = math.radians(angleInDegree)
print('Given angle :', angleInRadian)
print('cos(x) is :', math.cos(angleInRadian))
Output
Given angle : 1.0471975511965976 cos(x) is : 0.5000000000000001

math.sin()

It returns the sine of any number, in radians.

Example
snippet
import math
angleInDegree = 60
angleInRadian = math.radians(angleInDegree)
print('Given angle :', angleInRadian)
print('sin(x) is :', math.sin(angleInRadian))
Output
Given angle : 1.0471975511965976 sin(x) is : 0.8660254037844386

math.tan()

It returns the tangent of any number, in radians.

Example
snippet
import math
angleInDegree = 60
angleInRadian = math.radians(angleInDegree)
print('Given angle :', angleInRadian)
print('tan(x) is :', math.tan(angleInRadian))
Output
Given angle : 1.0471975511965976 tan(x) is : 1.7320508075688767
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +