Python Tutorial (33) – Example: Calculating the area of ​​a circle

Time: Column:Python views:201

Formula for the Area of a Circle

The formula to calculate the area of a circle is:

Area=πr2\text{Area} = \pi r^2

Where rr is the radius of the circle.

Example 1: Calculating the Area of a Circle

# Define a method to calculate the area of a circle
def findArea(r):
    PI = 3.142
    return PI * (r * r)

# Call the method
print("The area of the circle is %.6f" % findArea(5))

Output:

The area of the circle is 78.550000

Example 2: Using the math Module to Calculate the Area of a Circle

import math

def calculate_circle_area(radius):
    return math.pi * radius ** 2

# Example
radius = 5
area = calculate_circle_area(radius)
print(f"The area of a circle with radius {radius} is {area}")

In this example, a function calculate_circle_area is defined, which takes the radius as a parameter and returns the area of the circle.

Output:

The area of a circle with radius 5 is 78.53981633974483

You can modify the radius value in the above code to calculate the area of different circles.