In this tutorial, we will look at how to get the mathematical constant pi in python using the
How to get the 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:
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
# 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
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)
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
import math # get pi constant value print(math.pi)
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
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")
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
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.