Circle Class with Area and Circumference Calculation

Tags: Class, Object

Write a Python program to create a class Circle with an attribute for radius. Write methods calculate_area and calculate_circumference to calculate the area and circumference of the circle, respectively.

Example 1:

Input: circle = Circle(3)

Output: Area: 28.27, Circumference: 18.85

Example 2:

Input: circle = Circle(5)

Output: Area: 78.54, Circumference: 31.42

Define the class Circle and initialize radius in the __init__ function. Then, create the methods calculate_area and calculate_circumference and use the mathematical formulae for the area and circumference of a circle.

import math

class Circle:
    def __init__(self, radius):
        self.radius = radius
    def calculate_area(self):
        return round(math.pi * (self.radius ** 2), 2)
    def calculate_circumference(self):
        return round(2 * math.pi * self.radius, 2)

circle = Circle(3)
print('Area:', circle.calculate_area())  # Output: Area: 28.27
print('Circumference:', circle.calculate_circumference())  # Output: Circumference: 18.85

circle = Circle(5)
print('Area:', circle.calculate_area())  # Output: Area: 78.54
print('Circumference:', circle.calculate_circumference())  # Output: Circumference: 31.42

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!