In: Computer Science
Circle Object Python program
Compute the area and perimeter of a circle using Python classes. take the radius from the user and find the area and perimeter of a circle
please use comments to explain so i better understand
I have written the comments along with the code please go through them.
Code Snippet:
import math # for using pi
class Circle():
def __init__(self,radius): # init method or constructor
""" Initializing Circle class with radius as argument
"""
self.radius = radius # assigning radius to data member of circle class
def area(self):
""" Function calculates the circle area
Formula = pi * radius**2
"""
circle_area = math.pi * (self.radius**2) # calculating area of circle
return circle_area
def circumference(self):
""" Function calculate the circumference of the circle
Formula = 2 * pi* radius
"""
circle_circumference = 2 * math.pi * self.radius # calculating circumference of circle
return circle_circumference
def main():
circle = Circle(radius=float(input("Enter circle radius to calculate Area and Circumference: ")))
print("Area of Circle with radius {} is {}".format(circle.radius, circle.area()))
print("Circumference of Circle with radius {} is {}".format(circle.radius, circle.circumference()))
if __name__ == "__main__":
main()
Images of Code and Outputs:
Console Output:
Leave a comment for any further doubts.