Register Login

How to use pi in Python

Every student in their life has come across pi at least once. It's often used in mathematical formulas to calculate areas, perimeters, volume, circumference, etc.

Pi has an irreplaceable position in the following domains:

  1. quantum physics
  2. space flights
  3. medical procedures, and others.

In this article, we will learn how to use pi in Python and applications of pi in Python.

What is pi?

Pi is nothing but a mathematical constant. The approximate value of pi is 3.141592653589793. In mathematics, the Greek letter π represents pi. The pi (π ) is a constant of the math library in Python that returns the value 3.141592653589793. Pi helps calculate the area and circumference of the circle or other mathematical figures. The symbol π (pronounced out as "pi" or "pie") is a mathematical constant that represents a number equal to 3.14159 (for short). It is used in Euclidean geometry to define the ratio of a circle's circumference with respect to its diameter. Also, it has various equivalent illustrations. This unique number emerges in many formulas of the different verticals of mathematics and physics.

Use pi (π) in Python.

Let's take some examples to understand the application of pi (π) in Python:

Using math.pi

Math is a common module that comes with a lot of mathematical, geometric and trigometric built-in functions.
The first example will be using the math module. In Python, the math module allows access to several mathematical functions. The math module gets imported first to have access to the pi value.

Program:

# Importing math Library
import math
# This will print the value of pi in the output
print (math.pi)

OUTPUT:

The above example is the most standard way to call/get/use the value of pi (π)

Using NumPy for Pi

NumPy (Numeric Python) is a popular data science library that also comes with the Pi constant. One can also access the value of pi (π) using numpy.pi. The numpy module also allows access to several constants and mathematical functions and performs several mathematical operations on arrays. To access the pi (π) value using numpy, one must import the numpy module first.

Program:

import numpy
print(numpy.pi)

Output:

Using SciPy for Pi

SciPy (Scientific Python) is also a Python module that allows accessing the value of pi(π). The scipy module comes into use to solve mathematical, engineering, and technical problems. To access the pi (π) value using scipy, one must import the scipy module first.

Program:

import scipy
print(scipy.pi)

OUTPUT:

Output:

Even though they return the exact value of pi, Why Python has three modules to access pi (π)

The reason behind having three modules is that:

While using a module, say math or NumPy module, there is no need to import another module to access pi(π)

Access pi (π) using math.radians

Using math.radians is the most unusual way to access the value of pi (π). The function radians() converts degrees to radians. To access the pi (π) value using math.radian, one must import the math module first and enter the code as shown below:

Program:

import math
math.radians(180) # Here pi defines the 180 degree, passing the value 180 will print the value of pi (π)

Output:

Programs using Pi (π)

Below are some programs in Python that demonstrate the use of pi (π):

Area of a circle:

Code:

pi = 3.14 # The value of pi has been initialized in this code as 3.14
ra = float(input("Circle's radius:"))
Area = pi * ra * ra
print("Circles area: = %.2f" %Area)

Output:

Calculating the circumference of a circle:

Program:

pi = 3.14 #The value of pi taken in this code is 3.14
radius = float(input("Circle's radius:"))
Circumf =2 * pi * radius
print("Circumference of the circle is: = %.2f" %Circumf)

Output:

Wrapping up:

A lot of Python application requires mathematical operations and values to define the various formulae and engineering works such as civil and mechanical engineering. Such software often use the Pi (π) to calculate the area and circumference of the circle or other figures. In Python, pi is a math library constant that returns the value 3.141592653589793.

There are three modules available in Python for accessing the pi (π) value. The reason behind having three modules is to eliminate the need to import a separate module to access the value of pi (π). An unusual or unconventional method of accessing pi (π) is using math.radians.

The radians() convert degree to radians. Among all the above methods, the most efficient way of using the pi is to either use the math.pi or if you are using numpy or scipy module, then do not mention the math.pi separately for using pi constant.