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.

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: 0

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

How to find the perimeter of a rectangle in Python?

Below are different approaches to calculating the perimeter of a rectangle in Python.

  • Using Direct Formula Approach
  • Using Function

Using Direct Formula Approach

We use the direct formula approach to calculate the perimeter of the rectangle using Python. The perimeter of the rectangle can be calculated as: length + breadth + length + breadth. The formula for the perimeter of the rectangle = 2 × (Length + Breadth). After calculating the perimeter return the result.

Steps for Implementation

  1. We first take length and breadth as input parameters.
  2. Now, calculate the perimeter of the rectangle using the formula: 2 × (Length + Breadth).
  3. After calculating, print the result.

Implementation Code

# 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}")

Output

The perimeter of the rectangle with length 10 and breadth 5 is: 30

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

Using Function

We will use a function to calculate the perimeter of the rectangle. The logic and formula for calculating the perimeter of the rectangle are the same. We will use a function to reuse code later by simply changing the value.

Steps for Implementation

  1. We create a function to calculate the perimeter which calculates the perimeter of a rectangle using the formula 2 * (length + breadth).
  2.  Now, pass the input value length and breadth.
  3. Finally, we output the result.

Implementation Code

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

# Call the function
length = 10
breadth = 5
perimeter = calculate_perimeter(length, breadth)

print(f"The perimeter of the rectangle with length {length} and breadth {breadth} is: {perimeter}")

Output

The perimeter of the rectangle with length 10 and breadth 5 is: 30

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

Updated on: 2025-01-15T18:55:09+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements