Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Constants present in the ‘math’ module 
3.
Functional Utilities
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Math Module in Python

Author dhruv sharma
0 upvote
gp-icon
Basics of python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

In addition to the various powerful out-of-the-box capabilities that Python offers, such as supporting multi-paradigm programming features and styles, highly performant libraries and community support and an exuberant set of frameworks that are causing shifts and shaping new ideologies in the industry for developing production-grade software products blazingly quicker than before, the 'math' Python module aids one with an additional set of utility functions and constants that one can leverage for performing complex scientific and mathematical computations.

The principal divisions of the high-level mathematical calculations utilities that are built-in in the 'math' module include the following aspects:

  1. Constants
  2. Functional Utilities
     

Let's understand these functionalities with helpful examples covered in the article.

Also See, Intersection in Python, Swapcase in Python

Read About, Divmod in Python
 

Constants present in the ‘math’ module 

In Python, the math module has many constants for using frequently required scientific and mathematical values such as 'inf', 'pi', 'e' etc., while performing calculations that offers ease of use and enhanced precision. Some of these constants defined in the module are

  • Pi
  • Euler’s Constant (exponential value)
  • Tau
  • Infinity
  • Not a Number   

Pi
The 'pi' mathematical constant, which is equivalently represented as 22/7 or 3.14, generally is covered in the 'math' module as

# First importing the math Library
import math

# copying the value of pie in another variable
pie = math.pi

# diameter of the circle
d = 10

# circumference of the circle
print( pie * d )

Output:

31.41592653589793


Euler’s Constant
The 'e' mathematical constant, also known as the "Euler's Constant", which is equivalently represented as e = 2.718281…, generally is covered in the 'math' module as

# First importing the math Library
import math
 
# Then printing the value of the Euler number 'e.'
print (math. e)

Output:

2.718281828459045


Tau’s Constant
The 'τ' mathematical value, which is also a circle constant equivalent to 2π, generally is covered in the 'math' module as

# First importing the math Library
import math
 
# Then printing the value of tau
print (math.tau)

Output:

6.283185307179586


Infinity
The 'math' module has a constant equal to the floating-point positive infinity to represent an infinite value, i.e. 'math.inf'.

# First importing the math Library
import math

# Then printing the value of infinity
print (math.inf)

# Comparing float inf with math.inf
print(float("inf") == math.inf)

# Performing arithmetic addition between two infinity values
print(math.inf + math.inf)

Output:

inf
True
inf


Not-a-number (NaN)
To represent a nan value, the 'math' module has a constant that can be used to describe the notion of a quantity that is not a number as

# First importing the math Library
import math
 
# Then printing the value of nan constant in the math module
print (math.nan)

Output:

nan

 

You can practice by yourself with the help of online python compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Functional Utilities

So the other set of features that are also built-in to the 'math' module in Python is the different utility functions that it can use to compute multiple required values while performing heavy and 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.

  • Finding The Factorial
# First importing the math Library
import math
 
# using the factorial function to calculate factorial of 10
print(math.factorial(10))

Output:

3628800
  • Finding The Greatest Common Divisor of two numbers
# First importing the math Library
import math
 
# using the gcd function
print(math.factorial(4, 6))

Output:

2
  • 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))

Output:

The ceil of 10.9 is: 11
The floor of 10.9 is: 10


Power and Logarithmic functions
The power and logarithmic functions category contain the power, square root and other logarithmic functions.

  • Finding the square root
# First importing the math Library
import math
 
# using the square root function to calculate sqrt of 4
print(math.sqrt(4))

Output:

2.0
  • 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))

Output:

1024.0
  • Finding The exponentiated power
# First importing the math Library
import math
 
# e^9
print (math.exp(9))

Output:

8103.083927575384
  • Logarithmic Functions
# 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))

Output:

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


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))

Output:

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
  • 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))

Output:

The converted value from radians to degrees is: 45.0
The converted value from degrees to radians is: 0.7853981633974483


Special functions
This category covers all other frequently used functions, such as the gamma function. 

  • Finding gamma of a value
# First importing the math Library
import math
 
print(math.gamma(2))

Output:

1.0

Also read, Convert String to List Python

FAQs

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')))

Output:

False
False
True


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.

Want to learn Python but don't know where to start?

Start here. Coding Ninjas provide this fantastic course on Python. If you are getting into coding and want to build a strong foundation, this course is for you.

Also check out - Strong Number

You can also use Coding Ninjas Studio to practice various DSA questions asked in many interviews. It will assist you in mastering efficient coding techniques, and you will also get interview experiences from people working in big companies.

Previous article
Sys Module in Python
Next article
Using Heapq module in Python
Guided path
Free
gridgp-icon
Basics of python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass