Write a python program to find the area of a rectangle in python
In this exercise, you will learn how to find the area of rectangle in Python. There are different ways to calculate the area of a rectangle. Here, we have mentioned most of them-
If we know the width and height of a rectangle, then we can easily calculate the area of a rectangle using the following formula-
Area = Width * Height
The following Python program allows user to enter the width and height of a rectangle. Next, we are calculating the area as per the formula Area = width * height-
# Python Program to find Area of a Rectangle
width = float(input('Please enter width of a rectangle: '))
height = float(input('Please enter height of a rectangle: '))
# calculate the area
Area = width * height
print("\nArea of a Rectangle is: %.2f" %Area)
Output of the above code -
Please enter width of a rectangle: 31
Please enter height of a rectangle: 51
Area of a Rectangle is: 1581.00
Find area and perimeter of rectangle using function
The following program allows user to enter the width and height of a rectangle. We will pass these values to the function as arguments to calculate the area of a rectangle by using the formula Area = width * height. Next, we calculate the perimeter of a rectangle by using the formula 2 * (width + height). Finally, print the area and the perimeter of a rectangle.
def AreaOfRectangle(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)
width = float(input('Please Enter the Width of a Rectangle: '))
height = float(input('Please Enter the Height of a Rectangle: '))
AreaOfRectangle(width, height)
Find area of a rectangle using classes
In the given example, we have created a class called rectangle and used the __init__() method to initialise the values of that class. This method returns self.w*self.h which is the area of the class.
class rectangle():
def __init__(self,w,h):
self.w=w
self.h=h
def area(self):
return self.w*self.h
width=int(input("Please enter width of a rectangle:"))
height=int(input("Please enter height of a rectangle:"))
obj=rectangle(width,height)
print("Area of rectangle:",obj.area())
Output of the above code:
Please enter width of a rectangle:129
Please enter height of a rectangle:67
Area of rectangle: 8643
Python Area of Rectangle
If we know the width and height of a rectangle, then we can easily calculate the area of a rectangle using the following formula-
Area = Width * HeightThe following Python program allows user to enter the width and height of a rectangle. Next, we are calculating the area as per the formula Area = width * height-
Output of the above code -
Please enter width of a rectangle: 31 Please enter height of a rectangle: 51 Area of a Rectangle is: 1581.00Find area and perimeter of rectangle using function
The following program allows user to enter the width and height of a rectangle. We will pass these values to the function as arguments to calculate the area of a rectangle by using the formula Area = width * height. Next, we calculate the perimeter of a rectangle by using the formula 2 * (width + height). Finally, print the area and the perimeter of a rectangle.
Find area of a rectangle using classes
In the given example, we have created a class called rectangle and used the __init__() method to initialise the values of that class. This method returns self.w*self.h which is the area of the class.
Output of the above code:
need an explanation for this answer? contact us directly to get an explanation for this answerPlease enter width of a rectangle:129 Please enter height of a rectangle:67 Area of rectangle: 8643