find the area of a sector of a circle in python

main.py
import math

# function to calculate area of a sector
def sector_area(radius, angle):
    sector_area = (angle/360) * math.pi * radius ** 2
    return sector_area

# example usage
radius = 5
angle = 60 # in degrees
area = sector_area(radius, angle)
print("Area of the sector is:", area)
287 chars
13 lines

In the above code, we first import math module to use the value of pi. We then define a function sector_area that takes radius and angle (in degrees) as input and calculates the area of the sector. The formula used to calculate the area of a sector is: (angle/360) * pi * r^2.

We then call the function by passing radius and angle values and store the returned area value in a variable called area. Lastly, we print the value of area to the console.

related categories

gistlibby LogSnag