In this tutorial, you’ll learn how to get and use the value of pi in Python. We’ll explore a number of different ways in which you can get and store the value of pi in Python. First, we’ll look at the
math library, followed by the
NumPy library. We’ll explore why you may want to choose one way over the other and close out with a fun alternative way of generating the value.
What is the Pi Constant?
The number pi, π, is a mathematical constant that’s approximately equal to 3.14159. It’s commonly used in Euclidian geometry to represent the ratio of a circle’s circumference to its diameter.
Pi itself is an irrational number, meaning that the value cannot be represented as a common fraction. However, the fraction
22/7 is often used to represent its value as an approximation. Similarly, the decimal representation of the value never ends and never moves into a permanently repeating pattern.
Now that you have a good understanding of the nature and uses of pi, let’s see how we can get this important mathematical constant in Python!
Get Pi in Python Using Math
In this section, you’ll learn how to use the
math library to get the value of
pi in Python. Because the
math library is part of the standard Python library, you don’t need to install anything additional. Let’s see how we can import the value:
# Using math to Get the Value of Pi import math pi = math.pi print(pi) # Returns: 3.141592653589793
We can see that the constant pi is available simply by accessing the constant in the library.
If you’re only planning on using the constant from the library, it may make sense to import only that constant, rather than the whole library. This can be done as shown below:
# Only Importing Pi from Python math from math import pi pi_value = pi print(pi_value) # Returns: 3.141592653589793
This allows you to use the constant without needing to reference the library. In the next section, you’ll learn how to use the NumPy library to access the value of pi.
Get Pi in Python Using NumPy
Similar to the
math library, the Python NumPy library provides the value of the pi constant. Since NumPy isn’t part of the standard Python library, you may need to install it. This can be done by using either
conda, as shown below:
$ pip install numpy $ conda install numpy
Use either one of these installation methods, depending on your preferred package manager.
Once the library is installed, we can access the value of
pi by using the constant in the library:
# Getting the Value of Pi in NumPy import numpy as np pi_value = np.pi print(pi_value) # Returns: 3.141592653589793
This method works similarly to how we would use the
math library. Similarly, we can simply import the constant directly, if we only intend to use that value and nothing else from the library:
# Only Importing Pi from Python numpy from numpy import pi pi_value = pi print(pi_value) # Returns: 3.141592653589793
In the next section, we’ll explore when it’s better to use one method over the other.
Should You Use NumPy or Math to Get Pi in Python?
So far, you’ve learned two different ways to access the value of
pi. At this point, you may be wondering which method is better to use. Before diving into that discussion, let’s first take a look if the value of the two constants is equal.
We can do this by using the
== comparison operator:
# Comparing the two methods of getting pi in Python import math import numpy as np math_pi = math.pi numpy_pi = np.pi print(math_pi == numpy_pi) # Returns: True
Using the code above, we can see that the two values are the same. So, when would you use one over the other?
math library is part of the standard Python library, using this approach means you’re not loading any additional dependencies. However, if you’re working with numerical calculations, there’s a good chance you’re using numpy already. In this case, it may be more straightforward simply to use the numpy approach.
So, in conclusion, the best method to use is the one that’s most useful to your circumstance. If you’re already using
numpy in your program, you’re better off just using numpy’s
pi constant. If you’re not using numpy, however, and want to keep your dependencies low, then you should use
Get Pi in Python Using Radians
Another fun way that you can get the value of
pi in Python is to use the
radians() function from the
math library. When you pass in 180 as the value for the radian, the function returns the value of pi.
Let’s see what this looks like:
# Getting the Value of Pi with Radians import math pi = math.radians(180) print(pi) # Returns: 3.141592653589793
While this isn’t the most practical way to get the value of pi, it does work!
In this tutorial, you learned how to use Python to get the value of pi. You first learned about some of the basic properties of pi and why you may need a library to access its value. You then learned how to use both the
numpy packages to get the value of pi. Finally, you learned how to use the
radians() function to get the value of pi.
To learn more about related topics, check out the articles below: