Python Program to Find the Perimeter and Area of a Rectangle

Rectangles are one of the most commonly encountered shapes in geometry. Knowing how to calculate their perimeter and area is essential in various fields such as architecture, engineering, and graphic design. In this blog, we will cover how to write a Python program to calculate both the perimeter and area of a rectangle given its length and width.


Understanding the Formulas

To calculate the perimeter and area of a rectangle, we use the following formulas:

  1. Area:

    Where:
  • Length is one side of the rectangle.
  • Width is the adjacent side of the rectangle.
  1. Perimeter:

    The perimeter is the total distance around the rectangle, calculated by adding up the lengths of all four sides.

Steps to Write the Program

  1. Input the Length and Width: We’ll prompt the user to enter the dimensions of the rectangle.
  2. Apply the Formulas: Using the above formulas, we’ll compute the area and perimeter.
  3. Display the Results: Finally, the program will output the area and perimeter.

Python Program to Find the Perimeter and Area of a Rectangle

# Python program to calculate the perimeter and area of a rectangle

# Input: Asking the user to enter the length and width of the rectangle
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# Calculate the area using the formula Area = Length * Width
area = length * width

# Calculate the perimeter using the formula Perimeter = 2 * (Length + Width)
perimeter = 2 * (length + width)

# Output: Display the area and perimeter
print(f"The Area of the rectangle is: {area}")
print(f"The Perimeter of the rectangle is: {perimeter}")

Explanation:

  1. Input:
  • The user is prompted to input the length and width of the rectangle. We use float() to allow for decimal values.
  1. Calculation:
  • The area is calculated by multiplying the length and width: ( \text{Area} = \text{Length} \times \text{Width} ).
  • The perimeter is calculated by adding the length and width and then multiplying the result by 2: ( \text{Perimeter} = 2 \times (\text{Length} + \text{Width}) ).
  1. Output:
  • The program prints the area and perimeter of the rectangle using the print() function.

Sample Output

Test Case 1: Small Dimensions

Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
The Area of the rectangle is: 15.0
The Perimeter of the rectangle is: 16.0

Test Case 2: Larger Dimensions

Enter the length of the rectangle: 20
Enter the width of the rectangle: 10
The Area of the rectangle is: 200.0
The Perimeter of the rectangle is: 60.0

Understanding the Formulas

  • Area: The area of a rectangle is the space enclosed by its four sides. It is found by multiplying the length by the width. The units of area are always in square units, for example, square meters (( \text{m}^2 )) or square feet (( \text{ft}^2 )).
  • Perimeter: The perimeter of a rectangle is the total distance around the outside of the rectangle. It is calculated by adding the lengths of all four sides (two lengths and two widths) and can be understood as the boundary of the rectangle.

Handling Edge Cases

  1. Zero or Negative Values:
  • If the user enters zero or negative values for either the length or width, the area and perimeter calculations will still work mathematically, but these inputs may not make sense in practical applications. A rectangle cannot have negative dimensions.

Example:

Enter the length of the rectangle: -5
Enter the width of the rectangle: 3
The Area of the rectangle is: -15.0
The Perimeter of the rectangle is: -4.0

To handle this, we can add validation to ensure that both the length and width are positive:

# Python program to calculate the perimeter and area of a rectangle with validation

# Input: Asking the user to enter the length and width of the rectangle
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# Validation: Check if length and width are positive numbers
if length <= 0 or width <= 0:
    print("Invalid input! Length and width must be positive numbers.")
else:
    # Calculate the area and perimeter
    area = length * width
    perimeter = 2 * (length + width)

    # Output: Display the area and perimeter
    print(f"The Area of the rectangle is: {area}")
    print(f"The Perimeter of the rectangle is: {perimeter}")

Now, the program will handle incorrect inputs by displaying a message.


Practical Applications of Rectangle Calculations

Calculating the area and perimeter of a rectangle is essential in a variety of fields, including:

  • Architecture: When designing buildings, rooms, and spaces, knowing the area and perimeter of rectangular shapes is crucial.
  • Engineering: Many mechanical components, platforms, and surfaces are rectangular, requiring precise calculations for both area and perimeter.
  • Graphic Design: In design layouts or digital applications, understanding the space available is key to structuring designs.
  • Gardening or Construction: When creating layouts for gardens or buildings, calculating the perimeter helps in estimating the fencing, while the area helps in determining the amount of material required for covering the space.

Conclusion

In this blog post, we discussed how to write a Python program that calculates both the area and perimeter of a rectangle using basic geometric formulas. We also demonstrated how to handle edge cases with invalid inputs.

The program is simple but powerful, helping you practice basic input/output operations, mathematical calculations, and handling potential input errors in Python.

Feel free to extend this program by adding more shapes or improving the user experience with better error messages. Keep practicing, and happy coding!

Share with our team

Leave a Comment