Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Compute the Area of a Circle

Python Compute the Area of a Circle

Document Object Reference Manual 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 compute 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 of the above example is:

圆的面积为 78.550000

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

Example

import

math

def

calculate_circle_area

(

radius

)

:

   

return

math

.

pi

2

Example

radius

=

5

area

=

calculate_circle_area

(

radius

)

print

(

f

"The area of a circle with radius {radius} is {area}"

)

The example above defines a function calculate_circle_area that takes a radius as a parameter, computes and returns the circle's area.

In the example the radius is 5; you can change the radius value as needed to compute the areas of different circles. The code above produces the following output:

半径为 5 的圆的面积是 78.53981633974483

Document Object Reference Manual Python3 Examples