Value of pi in python

Get value of pi in python with np.pi and math.pi

In this tutorial, we will look at how to get the mathematical constant pi in python using the numpy and math libraries.

Value of pi in python

You can use the numpy library’s numpy.pi or the math standard library’s math.pi to get the value of the mathematical constant pi in python. The following is the syntax:

# using numpy
import numpy as np
# value of pi
np.py

# using math
import math
# value of pi
math.pi

Let’s look at each of the two methods with the help of examples.

Numpy is a scientific computation library in python and has values for a number of numerical constants including pi. You can use numpy.pi or np.pi depending on how you import the library to get the value of pi. Let’s print out the value of pi obtained from numpy.

import numpy as np

# get pi constant value
print(np.pi)

Output:

3.141592653589793

Here’s a list of all the constants that are available in numpy – Numpy constants.

You can also use the standard library math in python to get the value of pi. Let’s print out the value of pi obtained from math.pi

import math

# get pi constant value
print(math.pi)

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

3.141592653589793

We get the value of pi as a floating-point value.

Here’s a list of all constants available in the math library – math constants.

The values obtained from numpy.pi and math.pi appear to be the same. Let’s check if they are exactly equal.

import math
import numpy as np

# check if value of pi from both the libraries is equal
if math.pi == np.pi:
    print("Yes")
else:
    print("No")

Output:

Yes

We see that both the values are equal.

Now, should you prefer one over the other?

Well, an argument can be made that since math is a standard library in python, it makes sense to prefer it since it decreases the dependency on additional libraries. However, if you’re already using numpy for other operations/tasks in your code then it’s totally fine to use numpy.pi

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top