Table of Contents

Calculate Area and Perimeter of a Rectangle

Example Description
This Python code demonstrates how to calculate the area and perimeter of a rectangle using a function calcAreaPeri

Introduction:

This Python code demonstrates how to create a function to calculate the area and perimeter of a rectangle. The function accepts two parameters – length and breadth. It calculates the area and perimeter using the given formulae and returns a tuple containing the area and perimeter values. The user is prompted to enter the length and breadth of the rectangle, and the area and perimeter values are then displayed.

Code:

#Function to calculate area and perimeter of a rectangle
#The requirements are listed below:
 #1. The function should accept 2 parameters.
 #2. Calculate area and perimeter.
 #3. Return area and perimeter.
def calcAreaPeri(Length,Breadth):
 area = l*b 
 perimeter = 2*(l+b)
 #a tuple is returned consisting of 2 values area and perimeter
 return (area,perimeter) 
l = float(input("Enter length of the rectangle: "))
b = float(input("Enter breadth of the rectangle: "))
#value of tuples assigned in order they are returned
area,perimeter = calcAreaPeri(l,b) 
print("Area is:",area,"\nPerimeter is:",perimeter)

Logic:

  1. The function accepts two parameters – Length and Breadth.
  2. It calculates the area by multiplying the length and breadth and the perimeter by 2 times the sum of the length and breadth.
  3. The function returns a tuple containing the area and perimeter values.

Output:

Enter length of the rectangle: 100

Enter breadth of the rectangle: 80

>> Area is: 8000.0

>> Perimeter is: 360.0