by BehindJava
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
- User must enter the value of length and breadth.
- A class called rectangle is created and the init() method is used to initialise values of that class.
- A method called as area, returns self.length*self.breadth which is the area of the class.
- An object for the class is created.
- Using the object, the method area() is called with the parameters as the length and breadth taken from the user.
- The area is printed.