Find area of rectangle in Python
In this post, you will learn how to find the area of rectangle using the Python programming language. There are different ways to calculate the area of a rectangle. Here, we have mentioned most of them.
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 given formula.
Area = Width * Height
The following Python program allows the 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
Python find area and perimeter of rectangle using function
The given Python program allows the 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 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)
Python find area of a rectangle using classes
In the given Python program, we have created a class called 'rectangle' and used the __init__() method to initialize 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
Related Articles
Sum of even numbers in PythonFind square of a number in Python
Sum of n numbers in Python using while loop
Sum of n numbers in python using for loop
Python program to find area of circle
Program to find area of triangle in Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python program to bouncing ball in Pygame
Python iterate list with index
Python add list to list
Python random choice
Calculate a Percentage in Python
Remove character from string Python
Python raise keyword
Program to find area of triangle in Python
Print first 10 natural numbers using while loop in Python
Sum of n numbers in Python using while loop