Python Program to Find Area of Rectangle using Class | Function
Python program to find area of rectangle using class and function.
In this article, you will learn how to make a python program to find area of rectangle using class and function.
Example
Enter the length & width of the rectangle::
5
7
Area of rectangle = 35.0 units
You should have knowledge of the following topics in python programming to understand these programs:
- Python
input()
function - Python Oops
- Python Functions
An equation to Find the Area of a Rectangle
P = l * w
Where
P = perimeter
l = length of the rectangle
w = width of the rectangle
In this article, we solve this problem in three methods:
- Using the normal calculation
- Using the Class
- Using the function
Source Code
# Python Program to Find Area of Rectangle
print("Enter the length & width of the rectangle::\n")
l, w, a = float(input()), float(input()), None
# It will calculate the area of the rectangle
a = l * w
print("\nArea of rectangle = ", a, "units")
Output
Enter the length & width of the rectangle::
5
7
Area of rectangle = 35.0 units
Explanation
In this given program, we have taken inputs 5
& 7
from the user via the system console. Then we applied the formula to calculate the area of these values.
Then this will return the 35.0
units of the above calculation.
Python Program to Find Area of Rectangle using Class
# Python Program to Find Area of Rectangle using Class
class Calculation():
def __init__(self):
self.length = 0.0
self.width = 0.0
self.area = 0.0
def main(self):
print("Enter the length & width of the rectangle::\n")
self.length = float(input())
self.width = float(input())
self.calculateArea()
# It will calculate area of the rectangle
def calculateArea(self):
self.area = self.length * self.width
print("\nArea of rectangle = ", self.area, "units")
# It will create instance of the StudentData class
Calculation = Calculation()
Calculation.main()
Output
Enter the length & width of the rectangle::
45
23
Area of rectangle = 1035.0 units
Python Program to Find Area of Rectangle using Function
# Python Program to Find Area of Rectangle using Function
def CalculateArea():
print("Enter the length & width of the rectangle::\n")
l, w, a = float(input()), float(input()), None
# It will calculate the area of the rectangle
a = l * w
print("\nArea of rectangle = ", a, "units")
CalculateArea()