In this post, we will learn how to find the area of a circle using the Python Programming language.
As we know, the area of a circle is πr2, where r is the radius of the circle and π = 22/7 or 3.14.
Since π is a constant, we need to take only the radius value as input.
We can import the value of π using a math module or assign the value of π to a separate constant variable.
This tutorial takes the radius of the circle from the user and calculates the area of the circle using the following approaches:
- Using Constant π
- Using math Module
So, without further ado, let’s begin this tutorial.
Python Program to Find Area of Circle Using Constant π
# Python Program to Find Area of Circle Using Constant π PI = 3.14 r = float(input("Enter the radius of the circle: ")) area = PI * r * r print("Area of the circle = %.2f" %area)
Output
Enter the radius of the circle: 14
Area of the circle = 615.44
How Does This Program Work?
- In this program, we store the value of π in the PI named constant variable.
- Now, the user is asked to enter the radius of the circle. It is stored in the r named variable.
- The area of the circle is calculated using the formula πr2. The area of the circle is stored in the area named variable.
- The calculated area of the circle is printed up to 2 decimal places with the help of %.2f format specifier.
Python Program to Find Area of Circle Using math Module
In the above program, we have stored the value of π in a separate variable. To get more precise and accurate results, we will use the math module.
Python has a built-in module called math that provides access to different mathematical constants and functions. It also contains the value of π.
# Python Program to Find Area of Circle Using math Module import math r = float(input("Enter the radius of a circle: ")) area = math.pi * r * r print("Area of the circle = %.2f" %area)
Output
Enter the radius of a circle: 7
Area of the circle = 153.94
To use the value of π, use math.pi in the program.
Summary
After going through this post, I hope you understand how to find the area of a circle using the Python Programming language.