Python Program to find Area of a Rectangle

Write a Python Program to find Area of a Rectangle and Perimeter of a Rectangle with example. Before we step into the Python Program to find Area of a Rectangle example, Let see the definitions and formulas.

Python Area of a Rectangle

If we know the width and height then, we can calculate the area of a rectangle using below formula.

Area = Width * Height

Perimeter is the distance around the edges. We can calculate perimeter of a rectangle using below formula

Perimeter = 2 * (Width + Height)

Python Program to find Area of a Rectangle and Perimeter of a Rectangle

This program for Area of a rectangle allows the user to enter width and height of the rectangle. Using those values, this python program will calculate the Area of a rectangle and perimeter of a rectangle.

# Python Program to find Area of a Rectangle

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

# calculate the area
Area = width * height

# calculate the Perimeter
Perimeter = 2 * (width + height)

print("\n Area of a Rectangle is: %.2f" %Area)
print(" Perimeter of Rectangle is: %.2f" %Perimeter)
Python Program to find Area of a Rectangle

Following statements will allow the User to enter the Width and Height of a rectangle.

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

Next, we are calculating the area as per the formula

Area = width * height

In the next Python line, We are calculating the Perimeter of a rectangle

Perimeter = 2 * (width + height)

Following print statements will help us to print the Perimeter and Area of a rectangle

print("\n Area of a Rectangle is: %.2f" %Area)
print(" Perimeter of Rectangle is: %.2f" %Perimeter)

Python Program to find Area of a Rectangle using functions

This Python area of rectangle program allows the user to enter the width and height of a rectangle. We will pass those values to the function arguments to calculate the area of a rectangle.

# Python Program to find Area of a Rectangle using Functions

def Area_of_a_Rectangle(width, height):

    # calculate the area
    Area = width * height

    # calculate the Perimeter
    Perimeter = 2 * (width + height)

    print("\n Area of a Rectangle is: %.2f" %Area)
    print(" Perimeter of Rectangle is: %.2f" %Perimeter)

Area_of_a_Rectangle(6, 4)

Within this area of rectangle program, First, We defined the function with two arguments using def keyword. It means, User will enter the width and height of a rectangle. Next, We are Calculating the perimeter and Area of a rectangle as we described in our first example.


 Area of a Rectangle is: 24.00
 Perimeter of Rectangle is: 20.00
>>> Area_of_a_Rectangle(12, 9)

 Area of a Rectangle is: 108.00
 Perimeter of Rectangle is: 42.00
>>> 

Comments are closed.