Python is a feature-rich programming language where it provides different modules, libraries for mathematical calculations. PI is a special mathematical value that is mainly used to calculate circles area and similar values. As a popular mathematical value math.pi is provided after Python 1.4 .
Import and Use math.pi
PI value is provided with the of pi from math module. So in order to use PI in python the math module should be important before use.
Alternatively we can directly import the pi value from the math module and use directly without calling the math module name like below.
from math import pi print(pi)
Another alternative way to use pi value is renaming it and using with its new name. In the following example we will import the math.pi with the name of PI.
from math import pi as PI print(PI)
math.pi Type and Value
math.pi is a constant value which means it can not be change or read-only. We can only use this value but can not change because it is a mathematical constant. math.pi type is a float which is equal to
import math type(math.pi)
Calculate Area of a Circle
the most popular use case for the PI value is calculating the area of the circle. In order to calculate the area of a given circle, the PI and radius of the circle are required.
In the following example we will calculate the area of a circle which radius is 5 centimeter.
import math area = math.pi * math.pow(5,2) print(area) area = math.pi * 5 * 5 print(area)