Functional Utilities
Another set of features built into the 'math' module in Python is the various utility functions it provides for computing multiple required values during complex calculations. These can be broadly categorized as follows.
- Arithmetic Functions
- Power and Logarithmic functions
- Angular and Trigonometric functions
- Special functions
Arithmetic functions
The arithmetic function category contains various functions for performing arithmetic operations, such as finding factorial or the greatest common divisor of numbers.
# First importing the math Library
import math
# using the factorial function to calculate factorial of 10
print(math.factorial(10))

You can also try this code with Online Python Compiler
Run CodeOutput:
3628800

You can also try this code with Online Python Compiler
Run Code- Finding The Greatest Common Divisor of two numbers
# First importing the math Library
import math
# using the gcd function
print(math.factorial(4, 6))

You can also try this code with Online Python Compiler
Run CodeOutput:
2

You can also try this code with Online Python Compiler
Run Code- Finding The Floor and Ceil value
# First importing the math Library
import math
x = 10.9
# returning the ceil of 10.9
print ("The ceil of 10.9 is : ", end="")
print (math.ceil(x))
# returning the floor of 10.9
print ("The floor of 10.9 is : ", end="")
print (math.floor(x))

You can also try this code with Online Python Compiler
Run CodeOutput:
The ceil of 10.9 is: 11
The floor of 10.9 is: 10

You can also try this code with Online Python Compiler
Run Code
Power and Logarithmic functions
The power and logarithmic functions category contain the power, square root and other logarithmic functions.
# First importing the math Library
import math
# using the square root function to calculate sqrt of 4
print(math.sqrt(4))

You can also try this code with Online Python Compiler
Run CodeOutput:
2.0

You can also try this code with Online Python Compiler
Run Code- Finding The power of number raised to another value
# First importing the math Library
import math
# using the power function
print(math.pow(4,5))

You can also try this code with Online Python Compiler
Run CodeOutput:
1024.0

You can also try this code with Online Python Compiler
Run Code- Finding The exponentiated power
# First importing the math Library
import math
# e^9
print (math.exp(9))

You can also try this code with Online Python Compiler
Run CodeOutput:
8103.083927575384

You can also try this code with Online Python Compiler
Run Code# First importing the math Library
import math
# returning the log of 9,3
print ("The value of log 9 with base 3 is: ", math.log(9,3))
# returning the log base 2 of 8
print ("The value of log2 of 8 is : ", math.log2(8))
# returning the log10 of 100000
print ("The value of log10 of 100000 is : ", math.log10(10000))

You can also try this code with Online Python Compiler
Run CodeOutput:
The value of log 2 with base 3 is: 2.0
The value of log2 of 8 is: 3.0
The value of log10 of 100000 is: 5.0

You can also try this code with Online Python Compiler
Run Code
Angular and Trigonometric functions
Various trigonometric and angular functions such as 'sin', 'cosine' and 'tan' or functions such as 'degrees' etc., help perform calculations related to angle conversions or can also be used to convert the values of angles from degrees to radians or vice-versa.
- Finding sine, cosine or tangent
# First importing the math Library
import math
x = math.pi/4
# returning the value of sine of pi/4
print ("The value of sine of pi/4 is : ", math.sin(x))
# returning the value of the cosine of pi/4
print ("The value of cosine of pi/4 is : ", math.cos(x))
# returning the value of the tangent of pi/4
print ("The value of tangent of pi/4 is : ", math.tan(x))

You can also try this code with Online Python Compiler
Run CodeOutput:
The value of sine of pi/4 is: 0.7071067811865475
The value of the cosine of pi/4 is: 0.7071067811865475
The value of the tangent of pi/4 is: 0.9999999999999999

You can also try this code with Online Python Compiler
Run Code- Degrees to radians and back
# First importing the math Library
import math
x = math.pi/4
y = 45
# returning the converted value from radians to degrees
print ("The converted value from radians to degrees is: ", math.degrees(x))
# returning the converted value from degrees to radians
print ("The converted value from degrees to radians is: ", math.radians(y))

You can also try this code with Online Python Compiler
Run CodeOutput:
The converted value from radians to degrees is: 45.0
The converted value from degrees to radians is: 0.7853981633974483

You can also try this code with Online Python Compiler
Run Code
Special functions
This category covers all other frequently used functions, such as the gamma function.
# First importing the math Library
import math
print(math.gamma(2))

You can also try this code with Online Python Compiler
Run CodeOutput:
1.0

You can also try this code with Online Python Compiler
Run CodeFrequently Asked Questions
1. Are there functions to check NaN and infinite values in Python?
Yes, In Python, one can use the following utilities
import math
# checking isnan() values on math constants
print(math.isnan(math.tau))
print(math.isnan(math.inf))
# checking for NaN value
print(math.isnan(float('nan')))

You can also try this code with Online Python Compiler
Run CodeOutput:
False
False
True

You can also try this code with Online Python Compiler
Run Code
2. How are Python's various math module functions such as factorial and gcd fast?
Since various math module functions in Python are internally implemented in C. It provides access to the mathematical functions defined by the C standards, so you get fast results by using an efficient algorithm in C.
Key Takeaways
In this article, we learned about various functionalities offered by the math Python module, which included a set of mathematical and scientific constant values such as the Euler Number and Pi and saw different types of functions from arithmetic to some special functions.
Recommended Readings: