Python code calculates the area of a circle based on its radius using the formula ��2πr2, where �π is the mathematical constant representing the ratio of a circle’s circumference to its diameter. The code defines a function named calculate_circle_area that takes the radius as an argument, computes the area using the formula, and returns the result. In the example, the radius is set to 5, and the function is called to determine the area of a circle with that radius. The result is then printed with a formatted message, displaying the radius and calculated area rounded to two decimal places. This code serves as a reusable function for calculating the area of circles with different radii by providing the desired radius as an input parameter to the function.
Source code
import math
def calculate_circle_area(radius):
area = math.pi * radius**2
return area
# Example: Calculate the area of a circle with radius 5
radius = 5
area_of_circle = calculate_circle_area(radius)
print(f"The area of the circle with radius {radius} is {area_of_circle:.2f}")
Description
- Import the math Module:
Import mathThis line imports themathmodule, which provides mathematical functions, including the constant �π. - Define the Function:
def calculate_circle_area(radius):This line defines a function namedcalculate_circle_areathat takes a radius as a parameter. - Calculate the Area:
area = math.pi * radius**2Inside the function, this line calculates the area of the circle using the formula ��2πr2. - Return the Result:
return areaThe function returns the calculated area. - Example: Set Radius and Calculate Area:
radius = 5 area_of_circle = calculate_circle_area(radius)Here, a radius of 5 is assigned to the variableradius, and the functioncalculate_circle_areais called with this radius. The result is stored in the variablearea_of_circle. - Print the Result:
print(f"The area of the circle with radius {radius} is {area_of_circle:.2f}")Finally, a formatted message is printed, displaying the radius and the calculated area rounded to two decimal places.
This code can be easily modified to calculate the area of circles with different radii by changing the value assigned to the radius variable. The function enhances reusability, allowing for the calculation of circle areas in various contexts.
Screenshot

