How To Use Pi In Python

In this tutorial, we will be learning about the usage of Pi in Python. Pi is an essential mathematical constant, usually used in calculations related to circles and trigonometry. Pi is approximately equal to 3.14159265359.

So, understanding how to use and implement Pi in Python is beneficial for various mathematical and scientific computations. We will cover the use of the Python math library and numpy to work with Pi and explore some applications related to circle calculations.

Step 1: Importing the math library

To use the constant Pi in Python, we need to import the math library. This library provides access to mathematical functions and constants. To import the math library, you can use the following code:

After importing the library, you can use the constant Pi with the help of the math module.

Step 2: Accessing the Pi constant

To access the value of Pi, use the following code:

This code will output the value of Pi as given below:


You can also directly print the value of Pi by using math.pi as shown below:

Although the Pi value we have been using is an approximation, it is precise enough for most purposes and calculations.

Step 3: Calculating Circumference, Area, and Volume

Now let’s use Pi to calculate the circumference and area of a circle and the volume of a sphere. The formulae for these calculations are as follows:

  1. Circumference of a circle: 2 * Pi * radius
  2. Area of a circle: Pi * (radius ** 2)
  3. Volume of a sphere: (4/3) * Pi * (radius ** 3)

Let’s write a Python code to perform these calculations:

The output of this code will be:

Circumference: 18.84955592153876
Area: 28.274333882308138
Volume: 113.09733552923254

Step 4: Using NumPy for Pi

Aside from the math library, you can also use the numpy library to work with Pi. Numpy is a powerful library for numerical operations and provides the constant Pi as well. First, let’s install numpy using the following command:
pip install numpy
Now, we can import the numpy library in our Python code and access the value of Pi:

This code will output the same value of Pi as before:


Remember that you may use either the math library or numpy for Pi, depending on your preference and the specific requirements of your project.

Full code:


In this tutorial, we learned how to use the constant Pi in Python using the math and numpy libraries. We also demonstrated its practical application by calculating the circumference, area, and volume related to circles and spheres. Understanding the usage of Pi in Python is essential for any mathematical and scientific computation involving circles and trigonometric functions.