4 ways to find area or perimeter of rectangle

Python PythonBasic

In this post, we are going to understand 4 ways to find area or perimeter of rectangle with a python program code example that includes finding the area of a rectangle using class, function, the perimeter of a rectangle.

1. Simple Program to find area of a rectangle


In this Python program code, we will ask the user to enter the value of length and height of the rectangle.

We are using this mathematic formula To find the area of the rectangle Rect_area = width*height. Finally printing the calculated area of the rectangle.

Python Program Code

#Python Program to find area of a rectangle

length = float(input('Please Enter the length of a Rectangle: '))
height = float(input('Please Enter the height of a Rectangle: '))
Rect_area = length * height 
print("Calculated Rectangle of area is: %.2f" %Rect_area)

Output

Please Enter the length of a Rectangle: 14 
Please Enter the height of a Rectangle: 9
Calculated Rectangle of area is: 126.00

2. Find area of rectangle area and primeter


In this Python program code, we will ask the user to enter the value of length and height of the rectangle. To find the area and perimeter of a rectangle.

  • The perimeter of a rectangle is the area around the edge.The mathematical formula to find the perimeter of a rectangle is = 2*(Width + Height)
  • The formula to calculate rectangle area is := L*H
  • We have defined a function Rectangle_area(L, H) that takes two parameters, Length, and height of rectangle and calculates rectangle area and perimeter and print result output.

Python Program

#Python Program to find area of a rectangle using fuction   
 
def Rectangle_area(L,H):
        rect_area = L*H
        rect_prim =2*(L + H)
        print("calculated Rectangle area is:",rect_area)
        print("Primeter  of Rectangle is:",rect_prim)

length = float(input('Please Enter the length of a Rectangle: '))
height = float(input('Please Enter the width of a Rectangle: '))


Rectangle_area(length,height)

Output

Please Enter the length of a Rectangle: 12
Please Enter the width of a Rectangle: 13
calculated Rectangle area is: 156.0
Primeter  of Rectangle is: 50.0

3. Program to find area of a rectangle using class


In this Python program, we have defined a class Rect with a Rectangle_area() function that calculates the rectangle area.

To use this class We have created the Rect class object rectobj and called the function Rectangle_area() that will calculate the area of a rectangle and return the result.

Finally using the print() function to print the result. Let us understand with the below example.

Python Program

#Python Program to find area of a rectangle using class
class Rect:
    def __init__(self,l,w):
        self.l = l
        self.w = w
        
 
    def Rectangle_area(self):
        return self.l*self.w

length = float(input('Please Enter the length of a Rectangle: '))
height = float(input('Please Enter the height of a Rectangle: '))

rectobj = Rect(length,height )

print("calculated Rectangle area is:", rectobj.Rectangle_area())

Output

Please Enter the length of a Rectangle: 78
Please Enter the height of a Rectangle: 90
calculated Rectangle area is:7020.0

4. Find area of Rctangle using function


In this python program example, To find the area of a rectangle using the function we will define a function Rectangle_area() that has two parameters, lengths, and height of the rectangle and return the calculated area of a rectangle.

We have asked users to input the length and width of the rectangle and passed them to called function Rectangle_area() to find the result.

Python Program

def Rectangle_area(l,h):
        return l*h

length = float(input('Please Enter the length of a Rectangle: '))
height = float(input('Please Enter the width of a Rectangle: '))


print("calculated Rectangle area is:",Rectangle_area(length,height ))

Output

Please Enter the length of a Rectangle: 12
Please Enter the height of a Rectangle: 13
calculated Rectangle area is: 156.0

Summary

In this post, we have understood 4 ways to find the area or perimeter of a rectangle with a python program code example that includes how to find the area of a rectangle using class, How to find the area of a rectangle using the function, how to find the perimeter of a rectangle.