Python Calculate Circle Area

Document 对象参考手册Python3 Examples

The formula for the area of a circle is:

In the formula, r is the radius of the circle.

Example 1

# 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))

The output result of the above example is:

圆的面积为 78.550000

Use the math module to calculate the area of a circle:

Example

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}")

The above example defines a functioncalculate_circle_areathat takes the radius as a parameter, calculates and returns the area of the circle.

In the example, the radius is 5. You can modify the radius value as needed to calculate the area of different circles. The output result of the above code is:

半径为 5 的圆的面积是 78.53981633974483

Document 对象参考手册Python3 Examples

Other extensions