Python program to find perimeter of rectangle

A rectangle is a closed two-dimensional figure having 4 sides. The opposite sides are equal and parallel. The angle made by the adjacent side is equal to 90 degrees. The perimeter is the sum of all sides; in other words, the perimeter is two times the sum of length and breadth.

In this tutorial, we are going to learn how we can find the perimeter of a given rectangle in Python using different approaches ?

Formula of Perimeter of Rectangle

The formula for the perimeter of a rectangle is as follows ?

Perimeter of Rectangle = 2 × (length + breadth)

Where,
length ? is the horizontal distance.
breadth ? is the vertical distance.

Examples

Example 1

  • Input:
    length = 10 unit
    breadth = 5 unit
  • Output: 30 unit

Explanation

Using the formula to calculate perimeter: 2 × (Length + Breadth).
perimeter = 2 × (10 + 5)
= 2 × (15)
= 30 unit

Example 2

  • Input:
    length = 20 unit
    breadth = 0 unit
  • Output: Invalid rectangle

Explanation

If the length or breadth of any dimension is zero then a 2-dimensional figure does not exist. So, there is no perimeter of a non-existing rectangle figure.

Example 3

  • Input:
    length = 15 unit
    breadth = 10 unit
  • Output: 50 unit

Explanation

Using the formula to calculate perimeter: 2 × (length + breadth)
perimeter = 2 × (15 + 10)
= 2 × (25)
= 50 unit

Method 1: Using Direct Formula

We use the direct formula approach to calculate the perimeter of the rectangle. The perimeter can be calculated as: 2 × (Length + Breadth) ?

# Define length and breadth
length = 10
breadth = 5

# Calculate perimeter using the formula
perimeter = 2 * (length + breadth)

# Output
print(f"The perimeter of the rectangle with length {length} and breadth {breadth} is: {perimeter}")
The perimeter of the rectangle with length 10 and breadth 5 is: 30

Time Complexity: O(1)
Space Complexity: O(1)

Method 2: Using Function

We create a reusable function to calculate the perimeter. This approach is better for code organization and reusability ?

# Function to calculate the perimeter
def calculate_perimeter(length, breadth):
    return 2 * (length + breadth)

# Call the function with different values
length1, breadth1 = 10, 5
perimeter1 = calculate_perimeter(length1, breadth1)

length2, breadth2 = 15, 10
perimeter2 = calculate_perimeter(length2, breadth2)

print(f"Rectangle 1 - Length: {length1}, Breadth: {breadth1}, Perimeter: {perimeter1}")
print(f"Rectangle 2 - Length: {length2}, Breadth: {breadth2}, Perimeter: {perimeter2}")
Rectangle 1 - Length: 10, Breadth: 5, Perimeter: 30
Rectangle 2 - Length: 15, Breadth: 10, Perimeter: 50

Time Complexity: O(1)
Space Complexity: O(1)

Method 3: With Input Validation

A more robust approach that includes input validation to handle invalid dimensions ?

def rectangle_perimeter(length, breadth):
    # Validate input
    if length <= 0 or breadth <= 0:
        return "Invalid rectangle - dimensions must be positive"
    
    # Calculate perimeter
    perimeter = 2 * (length + breadth)
    return f"Perimeter: {perimeter} units"

# Test with different inputs
test_cases = [(10, 5), (20, 0), (15, 10), (-5, 3)]

for length, breadth in test_cases:
    result = rectangle_perimeter(length, breadth)
    print(f"Length: {length}, Breadth: {breadth} ? {result}")
Length: 10, Breadth: 5 ? Perimeter: 30 units
Length: 20, Breadth: 0 ? Invalid rectangle - dimensions must be positive
Length: 15, Breadth: 10 ? Perimeter: 50 units
Length: -5, Breadth: 3 ? Invalid rectangle - dimensions must be positive

Conclusion

The perimeter of a rectangle is calculated using the formula 2 × (length + breadth). Use functions for reusability and add input validation for robust programs. The time complexity is O(1) for all approaches.

Updated on: 2026-03-27T16:48:50+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements