0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate Perimeter of Circle

You can calculate the perimeter of a circle in Python using perimeter = 2 * 3.1416 * radius, where radius is the circle's radius.
📋

Examples

Inputradius = 1
OutputPerimeter of circle: 6.2832
Inputradius = 5
OutputPerimeter of circle: 31.416
Inputradius = 0
OutputPerimeter of circle: 0.0
🧠

How to Think About It

To find the perimeter of a circle, think about the distance around it. The formula is 2 * π * radius. So, you get the radius from the user, multiply it by 2 and π (about 3.1416), and that gives you the perimeter.
📐

Algorithm

1
Get the radius value from the user
2
Set π (pi) as 3.1416
3
Calculate perimeter by multiplying 2, π, and radius
4
Print the perimeter result
💻

Code

python
radius = float(input("Enter the radius of the circle: "))
pi = 3.1416
perimeter = 2 * pi * radius
print("Perimeter of circle:", perimeter)
Output
Enter the radius of the circle: 5 Perimeter of circle: 31.416
🔍

Dry Run

Let's trace the example where radius = 5 through the code

1

Input radius

User enters 5, so radius = 5.0

2

Set pi

pi = 3.1416

3

Calculate perimeter

perimeter = 2 * 3.1416 * 5.0 = 31.416

4

Print result

Output: Perimeter of circle: 31.416

StepVariableValue
1radius5.0
2pi3.1416
3perimeter31.416
4outputPerimeter of circle: 31.416
💡

Why This Works

Step 1: Get radius input

We use input() to get the radius from the user and convert it to a number with float() so we can do math.

Step 2: Use pi value

We set pi to 3.1416, which is a close value for π, the circle constant.

Step 3: Calculate perimeter

We multiply 2 * pi * radius to find the perimeter, which is the distance around the circle.

Step 4: Show result

We print the perimeter so the user can see the answer.

🔄

Alternative Approaches

Use math.pi constant
python
import math
radius = float(input("Enter the radius: "))
perimeter = 2 * math.pi * radius
print("Perimeter of circle:", perimeter)
This uses Python's built-in math.pi for more precise π value.
Define a function
python
def circle_perimeter(r):
    return 2 * 3.1416 * r
radius = float(input("Radius: "))
print("Perimeter:", circle_perimeter(radius))
Encapsulates calculation in a function for reuse.

Complexity: O(1) time, O(1) space

Time Complexity

The program does a fixed number of operations regardless of input size, so it runs in constant time.

Space Complexity

Only a few variables are used, so memory use is constant.

Which Approach is Fastest?

All approaches run instantly for any input size; using math.pi is preferred for accuracy without speed loss.

ApproachTimeSpaceBest For
Fixed pi valueO(1)O(1)Simple quick calculation
math.pi constantO(1)O(1)Accurate calculations
Function encapsulationO(1)O(1)Reusable code
💡
Use math.pi for better precision instead of a fixed number.
⚠️
Forgetting to convert input to float causes errors when multiplying.