Python Program to Create a Class and Compute the Area and the Perimeter of the Circle


When it is required to find the area and perimeter of a circle using classes, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to find the area and perimeter of the circle.

Below is a demonstration for the same −

Example

 Live Demo

import math
class circle_compute():
   def __init__(self,my_radius):
      self.radius=my_radius
   def area_calculate(self):
      return math.pi*(self.radius**2)
   def perimeter_calculate(self):
      return 2*math.pi*self.radius
my_result = int(input("Enter the radius of circle..."))
my_instance = circle_compute(my_result)
print("The radius entered is :")
print(my_result)
print("The computed area of circle is ")
print(round(my_instance.area_calculate(),2))
print("The computed perimeter of circle is :")
print(round(my_instance.perimeter_calculate(),2))

Output

Enter the radius of circle...7
The radius entered is :
7
The computed area of circle is
153.94
The computed perimeter of circle is :
43.98

Explanation

  • A class named ‘circle_compute’ class is defined, that has functions like ‘area_calculate’, ‘perimeter_calculate’.
  • These are used to calculate the area and perimeter of a circle respectively.
  • An instance of this class is created.
  • The value for radius is entered and operations are performed on it.
  • Relevant messages and output is displayed on the console.

Updated on: 11-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements