Python Program to Calculate the Area of a Circle: With and Without User Input
Learn how to calculate the area of a circle in Python with step-by-step examples. Includes programs that take user input and programs with predefined values.
Calculating the area of a circle is a common programming exercise. In Python, you can do this in two main ways:
-
Using user input – the program asks the user to enter the radius.
-
Without user input – the program uses a predefined radius value.
This guide explains both approaches with clear examples.
Formula for Area of a Circle
The area (A) of a circle is calculated using:
A=π×r2
Where:
-
(r) = radius of the circle
-
(\pi) = approximately 3.14159 (Python can use the
math.piconstant)
1. Python Program with User Input
This version lets the user enter the radius:
import math
# Ask the user to enter the radius
radius = float(input("Enter the radius of the circle: "))
# Calculate area
area = math.pi * radius ** 2
# Display the result
print("The area of the circle is:", area)
Explanation:
-
input()gets the user input as a string. -
float()converts it to a number with decimals. -
math.piprovides a precise value of π. -
radius ** 2calculates the square of the radius.
2. Python Program Without User Input
This version uses a predefined radius:
import math
# Predefined radius
radius = 5
# Calculate area
area = math.pi * radius ** 2
# Display the result
print("The area of the circle with radius", radius, "is:", area)
Explanation:
-
The radius is set directly in the code.
-
Useful for testing, automation, or when user input is not needed.
Key Points to Remember
-
Always use float for decimal precision when calculating areas.
-
Python’s
math.pigives a more accurate π value than manually typing 3.14. -
Using user input makes your program interactive.
-
Predefined values make it simpler for automated calculations.
Python Program Output Examples
With User Input:
Enter the radius of the circle: 7
The area of the circle is: 153.93804002589985
Without User Input:
The area of the circle with radius 5 is: 78.53981633974483
Python Program Variations
You can also create:
-
Formatted output to 2 decimal places:
print("The area of the circle is: {:.2f}".format(area))
-
Function-based program for reuse:
def circle_area(r):
import math
return math.pi * r ** 2
print(circle_area(10))
Python Circle Area FAQs
Q1: Can I use 3.14 instead of math.pi?
Yes, but math.pi is more precise.
Q2: Can I calculate the area without importing math?
Yes, by manually setting pi = 3.14159.
Q3: How do I make the program accept integer and decimal inputs?
Using float(input()) handles both integers and decimal numbers.
Final Thoughts
Calculating the area of a circle in Python is a simple yet important exercise for beginners. By understanding both user input and predefined values, you can make interactive or automated programs.
CroszEduVerse aims to make Python tutorials clear, practical, and ready for beginners or students who want real-world coding examples.
What's Your Reaction?