Using mathematical functions is an integral part of any programming task. Python provides a host of mathematical functions. All the mathematical functions are provided through the module called "math".
The math module offers mathematical constants as well as mathematical functions. The categories of mathematical functions include basic functions, trigonometrical functions, logarithmic functions and so on.
To invoke any mathematical function either on the python interpreter console or inside a python program with extension .py, the math module needs to be imported.
import math |
Mathematical constants from Python
pi(π) |
The value of Pi(π) is 3.141592653589793 |
e |
The value of the mathematical constant e is 2.718281828459045 |
Frequently Used Mathematical Functions in Python
Returns the absolute value of an integer or a floating point number |
|
Ceil returns next integer bigger than the given number |
|
Floor returns next integer smaller than the given number |
|
Truncates a floating point number and returns the integer part |
|
The number is split into to a fractional part and an integer part and the values are returned. |
|
Returns the value of aNumber_in raised to the power aPower_in. |
|
Returns the square root value of the given number. |
|
Returns the greatest common devisor of two integers |
|
Converts an angular value specified in radians into degrees |
|
Converts an angular value specified in degrees into radians |
|
Returns the distance between origin i.e., (0,0) and the specified point. |
|
Sine theta value i.e, ratio of opposite side to the hypotenuse is returned. |
|
Cos theta value i.e, ratio of adjacent side to the hypotenuse is returned. |
|
Tan theta value i.e, ratio of Cos theta to the Sine theta is returned. |
Example program using functions from python math module
import math
#Find 2 to the power 5 math.pow(2,5)
#Find square root of 32 squareRoot = math.sqrt(32) print("Square root of 32:{}".format(squareRoot))
#Find GCD of 32, 58 integer1 = 32 integer2 = 58 gcdValue=math.gcd(32, 58) print("GCD of numbers {} {}:{}".format(integer1,integer2,gcdValue))
#Find ceiling of 9.99 findCeilFor = 9.99 ceiling = math.ceil(findCeilFor)
print("Ceiling of {}:{}".format(findCeilFor,ceiling))
#Find floor of 100.25 findFloorFor = 100.25 floor = math.floor(100.25) print("Floor of {}:{}".format(findFloorFor,floor)) |
Output
Square root of 32:5.656854249492381 GCD of numbers 32 58:2 Ceiling of 9.99:10 Floor of 100.25:100
|