Python Math Module
In python, the math module is useful to perform mathematical related operations. The math module has different functions (trigonometric, logarithmic, conversion, etc.) and constants to perform various mathematical tasks.
Math Constants
The following table lists the constants available in the python math module.
Following is the example of using the math module constants in python.
# Print the value of E
print ("E val:", math.e)
# Print the positive infinity
print ("inf val:", math.inf)
# Print the negative infinity
print ("Neg inf val:", -math.inf)
# Print the value of nan
print ("nan val:", math.nan)
# Print the value of pi
print ("pi val:", math.pi)
# Print the value of tau
print ("tau val:", math.tau)
The above python math module constants example will return the result as shown below.
inf val: inf
Neg inf val: -inf
nan val: nan
pi val: 3.141592653589793
tau val: 6.283185307179586
Math Functions
Same as constants, the math module has different methods to perform mathematical operations.
Following is the example of using math module functions to perform different operations in python.
# Print the square root of number
print (math.sqrt(64))
# Rounds number up to nearest integer
print (math.ceil(5.647))
# Rounds number down to nearest integer
print (math.floor(5.647))
# Print the power of number
print (math.pow(5,3))
# Print the sine of a number
print (math.sin(60))
# Print the cosine of a number
print (math.cos(60))
# Print the tangent of a number
print (math.tan(60))
The above python math module example will return the result as shown below.
6
5
125.0
-0.3048106211022167
-0.9524129804151563
0.320040389379563
The following table lists the different methods available in the math module to perform logarithmic, trigonometric, etc., operations based on our requirements.
