Using Pi in Python with Numpy, Scipy and Math Library

Using Pi in Python

A very trivial scenario that you will come across in coding is using the value of Pi in Python language. And as it turns out, there are multiple libraries Numpy (np.pi) , Scipy (scipy.pi), and Math (math.pi) with which you can use the value of Pi in Python. Let us see them one by one.

i) Pi in Numpy – np.pi

Here we import the Numpy library as np. And the value of pi can be accessed by using np.pi as shown in the example below.

In [0]:

import numpy as np

print('Value of Pi using np.pi is', np.pi);


Value of Pi using np.pi is 3.141592653589793


ii) Pi in Scipy – scipy.pi

Here the Scipy library is imported and then the value of pi is accessed by using scipy.pi as shown below.

In [1]:

import scipy

print('Value of Pi using scipy.pi is', scipy.pi);


Value of Pi using scipy.pi is 3.141592653589793


iii) Pi in Math – math.pi

Finally, we can use the math library of python to use the value of pi with math.pi as explained in the below example.

In [2]:

import math

print('Value of Pi using math.pi is', math.pi);


Value of Pi using math.pi is 3.141592653589793


np.pi vs scipy.pi vs math.pi

We saw three ways of using pi in Python. So let us first confirm if the value of pi returned by them are all same.

In the below snippet we are comparing the value of all three and the output confirms that the value of Pi is the same for them.


if np.pi == scipy.pi == math.pi:
  print('Values of Pi same')
  print('Values of Pi is NOT same')


Values of Pi same


So now the question arises which of the three options we should use in Python if the value of Pi is the same in each of the three. Well, it does not really matter, you can use any option, but it is ideal to use the pi from the library if you are already using it in your code. E.g. if you are already working with the numpy code then it will make more sense to use np.pi instead of the other two options.


Hope you liked our quick tutorial on using the value of Pi in Python with Numpy, Scipy, and Math libraries. You can use any of the three approaches, it is just a matter of convenience.

  • MLK

    MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.

    View all posts

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *