In this python tutorial, you will learn about the Python program to find an area of a rectangle and, also we will check:
- How to find an area of a rectangle in Python
- Python program to calculate the area of a rectangle using function
- How to find area and perimeter of a rectangle in Python
- Python program to find the area and perimeter of a rectangle using function
- How to find the area of a rectangle using classes in Python
Python program to find an area of a rectangle
Let see python program to find an area of a rectangle.
- Firstly, we will take input from the user for length and breadth using the input() function.
- Now, we will calculate the area of a rectangle by using the formula Area = l * b.
- At last, print the area of a rectangle to see the output.
Example:
l = float(input('Enter the length of a Rectangle: '))
b = float(input('Enter the breadth of a Rectangle: '))
Area = l * b
print("Area of a Rectangle is: %.2f" %Area)
You can refer to the below screenshot to see the output for python program to find an area of a rectangle.
The code we can use to find an area of a rectangle in Python.
Also, check, How to calculate simple interest in Python and How to find area of a triangle in Python?
Python program to calculate area of a rectangle using function
Here, we will see python program to calculate the area of a rectangle using function.
- Firstly, we will define a function with two arguments using def keywords.
- Now, we will calculate the area of a rectangle inside the function by using the formula Area = width * height.
- The user will enter the width and height of a rectangle and we will pass those values to the function arguments.
- At last, print the area of a rectangle to see the output.
Example:
def AreaofRectangle(width, height):
Area = width * height
print("Area of a Rectangle is: %.2f" %Area)
AreaofRectangle(8, 6)
You can refer to the below screenshot to see the output for the python program to calculate the area of a rectangle using function.
The above code, we can use to calculate area of a rectangle using function in Python.
Check out, Python program to print pattern.
Python program to find area and perimeter of a rectangle
Now, we will see python program to find area and perimeter of a rectangle.
- Firstly, we will take input from the user for length and breadth using the input() function.
- Now, we will calculate the area of a rectangle by using the formula Area = w * h.
- Next, we are calculating the perimeter of a rectangle Perimeter = 2 * (w + h)
- At last, print the area and perimeter of a rectangle to see the output.
Example:
w = float(input('Enter the Width of a Rectangle: '))
h = float(input('Enter the Height of a Rectangle: '))
Area = w * h
Perimeter = 2 * (w + h)
print("Area of a Rectangle is: %.2f" %Area)
print("Perimeter of Rectangle is: %.2f" %Perimeter)
You can refer to the below screenshot to see the output for the python program to find area and perimeter of a rectangle.
The above code, we can use to find area and perimeter of a rectangle in Python.
You may also like, Python program to find the area of square.
Python program to find the area and perimeter of a rectangle using function
Let’s see python program to find the area and perimeter of a rectangle using function.
- Firstly, we will define a function with two arguments using def keywords.
- Now, we will calculate the area of a rectangle inside the function by using the formula Area = width * height.
- Next, we are calculating the perimeter of a rectangle inside the function using the formula Perimeter = 2 * (width + height).
- Allow the user to enter the width and height of a rectangle by using the input function and we will pass those values to the function arguments.
- At last, print the area and perimeter of a rectangle to see the output.
Example:
def Areaofrectangle(width, height):
Area = width * height
Perimeter = 2 * (width + height)
print("Area of a rectangle is: %.2f" %Area)
print("Perimeter of rectangle is: %.2f" %Perimeter)
width = float(input('Enter the width of a rectangle: '))
height = float(input('Enter the height of a rectangle: '))
Areaofrectangle(width, height)
You can refer to the below screenshot to see the output for python program to find the area and perimeter of a rectangle using function.
The above Python code we can use to find the area and perimeter of a rectangle using function.
Read, How to print factorial of a number in Python.
Python program to find area of a rectangle using classes
Let see python program to find the area of a rectangle using classes.
- Firstly, we will create a class called rectangle and the __init__() method is used to initialize values of that class.
- A method called area(self) return self.length*self.breadth which is the area of the class.
- The user will enter the value of length and breadth by using the input function.
- An object for the class is created as obj=rectangle(l,b)
- By using the object, the method area() is called with the perimeters as length and breadth taken from the user.
- At last, print the area of a rectangle to see the output.
Example:
class rectangle():
def __init__(self,length,breadth):
self.length=length
self.breadth=breadth
def area(self):
return self.length*self.breadth
l=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(l,b)
print("Area of rectangle:",obj.area())
You can refer to the below screenshot to see the output for python program to find the area of a rectangle using classes.
This is the Python program to find area of a rectangle using classes.
You may like the following Python tutorials:
- How to calculate area of a circle in Python
- How to Print Python Fibonacci series
- How to swap two numbers in Python
- How to subtract two numbers in Python
- How to divide two numbers in Python
- PdfFileReader Python example
In this Python tutorial, we have learned about the Python program to find an area of a rectangle. Also, we covered these below topics:
- Python program to find an area of a rectangle
- Python program to calculate the area of a rectangle using function
- Python program to find area and perimeter of a rectangle
- Python program to find the area and perimeter of a rectangle using function
- Python program to find the area of a rectangle using classes
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.