To find the area of a rectangle or a square you need to multiply the length and the width of a rectangle or a square. There are different units for perimeter and area. The perimeter has the same units as the length of the sides of rectangle or square whereas the area’s unit is squared.
You May Also Like: Python Program to Calculate Area and Perimeter of Rectangle in Tkinter
In this example. I’ll show how to calculate Area and Perimeter of Rectangle in Python.
Python Code:
1 2 3 4 5 6 7 8 |
l=int(input("Length : ")) w=int(input("Width : ")) area=l*w perimeter=2*(l+w) print("Area of Rectangle : ",area) print("Perimeter of Rectangle : ",perimeter) |
- Save the code to a file or type it into a text editor.
- Open a terminal or command prompt.
- Navigate to the directory where the code is saved.
- Run the command
python filename.py
, wherefilename.py
is the name of the file containing the code. - The program will prompt you to enter the length and width of the rectangle. Type in values for these variables and press enter.
- The program will then calculate the area and perimeter of the rectangle using the formula provided in the code, and print the results to the terminal.
For example, if you enter 3 for the length and 4 for the width, the output will be:
Area of Rectangle : 12 Perimeter of Rectangle : 14
This code calculates the area and perimeter of a rectangle given its length and width.
You may also like: Python Tutorial with Exercises(100+ Examples)
Here’s a brief explanation of how the code works:
The user is prompted to enter the length and width of the rectangle.
The “area” variable is assigned the value of the rectangle’s area, which is calculated by multiplying the length and width.
The “perimeter” variable is assigned the value of the rectangle’s perimeter, which is calculated by multiplying the sum of the length and width by 2.
The area and perimeter of the rectangle are printed to the console.
[…] Calculate Area and Perimeter of Rectangle in Python […]
gooooood
[…] You May Also Like: Python Program to Calculate Area and Perimeter of Rectangle […]
NICE