Python math.pi Constant



The Python math.pi constant is used to represent the mathematical constant Π (pi), which is approximately equal to 3.14159. It is a predefined value available in Python's math module and is commonly used in mathematical calculations involving circles, spheres, trigonometry, and other geometric computations.

Mathematically, Π is a special number that represents the ratio of the circumference of a circle to its diameter. No matter how big or small the circle is, if you divide its circumference by its diameter, you will always get Π. It is an irrational number, which means it cannot be expressed as a simple fraction and its decimal representation goes on infinitely without repeating.

Syntax

Following is the basic syntax of the Python math.pi constant −

math.pi

Return Value

The constant returns the value of PI.

Example 1

In the following example, we are using the math.pi constant to calculate the area of a circle given its radius −

import math
radius = 5
area = math.pi * (radius ** 2)
print("The area of the circle with radius", radius, "is:", area)

Output

Following is the output of the above code −

The area of the circle with radius 5 is: 78.53981633974483

Example 2

Here, we are calculating the circumference of a circle given its radius −

import math
radius = 8
circumference = 2 * math.pi * radius
print("The circumference of the circle with radius", radius, "is:", circumference)

Output

The output obtained is as follows −

The circumference of the circle with radius 8 is: 50.26548245743669

Example 3

In this example, we are calculating the volume of a sphere given its radius −

import math
radius = 4
volume = (4/3) * math.pi * (radius ** 3)
print("The volume of the sphere with radius", radius, "is:", volume)

Output

The result produced is as follows −

The volume of the sphere with radius 4 is: 268.082573106329

Example 4

Now, we calculate the length of an arc by providing the radius of the circle and the central angle of the arc in degrees. We use the formula for the length of an arc, which is the radius multiplied by the central angle (in radians) (r * θ). Then, we convert the angle from degrees to radians using the math.radians() method before performing the calculation −

import math
radius = 10
angle_in_degrees = 45
angle_in_radians = math.radians(angle_in_degrees)
arc_length = radius * angle_in_radians
print("The length of the arc with radius", radius, "and angle", angle_in_degrees, "degrees is:", arc_length)

Output

We get the output as shown below −

The length of the arc with radius 10 and angle 45 degrees is: 7.853981633974483
python_maths.htm
Advertisements