Programming Code Examples

Python Program to Calculate Area and Perimeter of Rectangle2 min read

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. Save the code to a file or type it into a text editor.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the code is saved.
  4. Run the command python filename.py, where filename.py is the name of the file containing the code.
  5. The program will prompt you to enter the length and width of the rectangle. Type in values for these variables and press enter.
  6. 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.

2 comments