- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Find the Area of a Rectangle Using Classes
When it is required to find the area of a rectangle using classes, object oriented method is used. Here, a class is defined, 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 of rectangle.
Below is a demonstration for the same −
Example
class shape_rectangle(): def __init__(self,my_length, my_breadth): self.length = my_length self.breadth = my_breadth def calculate_area(self): return self.length*self.breadth len_val = 6 bread_val = 45 print("The length of the rectangle is : ") print(len_val) print("The breadth of the rectangle is : ") print(bread_val) my_instance = shape_rectangle(len_val,bread_val) print("The area of the rectangle : ") print(my_instance.calculate_area()) print()
Output
The length of the rectangle is : 6 The breadth of the rectangle is : 45 The area of the rectangle : 270
Explanation
- A class named ‘shape_rectangle’ is defined.
- It has the ‘init’ method that initializes values.
- It also has a method that calculates the area of the rectangle given the specific parameters.
- An instance of this class is created.
- The function to calculate area is called by passing required parameters.
- It is displayed as output on the console.
- Related Articles
- Golang Program to Find the Area of a Rectangle Using Classes
- Java program to find the area of a rectangle
- Golang program to find the area of a rectangle
- Swift Program to find the area of the rectangle
- Program to find largest rectangle area under histogram in python
- Python Program to find the area of a circle
- Program to find area of a polygon in Python
- Python Program to Append, Delete and Display Elements of a List Using Classes
- Swift Program to Find the Perimeter of a Rectangle
- Java Program to Find the Perimeter of a Rectangle
- Kotlin Program to Find the Perimeter of a Rectangle
- If the length of a rectangle is tripled and its breadth is doubled, what will happen to the area of this rectangle? Find the ratio of the area of the resultant to the area of the original rectangle.
- Program to find the Area of an Ellipse using C++
- The length of a rectangle is 50 cm if the perimeter of the rectangle is 150 cm. Find the area of the rectangle.
- C program to print area of triangle, square, circle, rectangle and polygon using switch case.

Advertisements