
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- We first take length and breadth as input parameters.
- Now, calculate the perimeter of the rectangle using the formula: 2 Ã (Length + Breadth).
- 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
- We create a function to calculate the perimeter which calculates the perimeter of a rectangle using the formula 2 * (length + breadth).
- Now, pass the input value length and breadth.
- 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)