by BehindJava

Write a program to find the Area of a Rectangle Using Classes in Python

Home » python » Write a program to find the Area of a Rectangle Using Classes in Python

In this tutorial we are going to learn about finding the Area of a Rectangle Using Classes in Python.

Python Program to Find the Area of a Rectangle Using Classes

class rectangle():
    def __init__(self,breadth,length):
        self.breadth=breadth
        self.length=length
    def area(self):
        return self.breadth*self.length
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",obj.area())
 
print()

Program Explanation

  1. User must enter the value of length and breadth.
  2. A class called rectangle is created and the init() method is used to initialise values of that class.
  3. A method called as area, returns self.length*self.breadth which is the area of the class.
  4. An object for the class is created.
  5. Using the object, the method area() is called with the parameters as the length and breadth taken from the user.
  6. The area is printed.