In this tutorial, we will learn how to use **Pi** in **Python Numpy**. Pi is a mathematical constant approximately equal to 3.14159.

Numpy, a library in Python, is widely used for working with arrays, performing mathematical and logical operations, and much more. Numpy also provides useful constants like Pi, making your code more efficient and easier to understand.

Let’s explore how to work with Pi using the Numpy library in Python.

### Step 1: Install and Import Numpy

First, we need to have the Numpy library installed on our system. You can install it via pip, a package installer for Python:

pip install numpy

Once Numpy is installed, we can import it into our Python code:

1 |
import numpy as np |

We are using the keyword `as`

to create an alias `np`

for Numpy, which is a common practice in the Python community.

### Step 2: Access the Value of Pi

To access the value of Pi, simply use the constant `np.pi`

:

1 2 |
pi_value = np.pi print("Value of Pi: ", pi_value) |

**Output**

Value of Pi: 3.141592653589793

### Step 3: Perform Mathematical Operations Using Pi

You can use Numpy’s Pi constant in various mathematical operations. Here are some examples:

#### Calculating the circumference and area of a circle

Given the radius of a circle, the circumference can be calculated using the formula:

Circumference = 2 * pi * radius

And the area can be calculated using the formula:

Area = pi * radius^2

Let’s use Numpy to calculate the circumference and area of a circle with a radius of 5 units:

1 2 3 4 5 6 |
radius = 5 circumference = 2 * np.pi * radius area = np.pi * radius**2 print("Circumference of the circle: ", circumference) print("Area of the circle: ", area) |

**Output**

Circumference of the circle: 31.41592653589793 Area of the circle: 78.53981633974483

### Step 4: Use Pi in Numpy Functions

Numpy has many built-in functions that accept Pi as an input. For example, the `sin()`

and `cos()`

functions can be used with Pi to find the sine and cosine of an angle in radians.

Let’s find the sine and cosine of Pi radians:

1 2 3 4 5 |
sin_pi = np.sin(np.pi) cos_pi = np.cos(np.pi) print("Sine of Pi radians: ", sin_pi) print("Cosine of Pi radians: ", cos_pi) |

**Output**

Sine of Pi radians: 1.2246467991473532e-16 Cosine of Pi radians: -1.0

**Note:** The sine value is not exactly zero due to floating-point precision limitations.

## Full code

Here is the full code used in this tutorial:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import numpy as np # Access the value of Pi pi_value = np.pi print("Value of Pi: ", pi_value) # Perform mathematical operations using Pi radius = 5 circumference = 2 * np.pi * radius area = np.pi * radius**2 print("Circumference of the circle: ", circumference) print("Area of the circle: ", area) # Use Pi in Numpy functions sin_pi = np.sin(np.pi) cos_pi = np.cos(np.pi) print("Sine of Pi radians: ", sin_pi) print("Cosine of Pi radians: ", cos_pi) |

## Conclusion

In this tutorial, we have learned how to use Pi in Python Numpy. We demonstrated how to access and use the Pi constant for various mathematical operations and Numpy functions. This knowledge will help you perform calculations accurately and efficiently using the Numpy library in Python.