Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find perimeter of square
A square is a closed two-dimensional figure having 4 equal sides. Each angle of a square is 90 degrees. The perimeter of a square is the sum of all its sides.
Problem Description
In this problem, we are given the side of a square, and we have to find the perimeter of the square. In this tutorial, we are going to find the perimeter of a given square in Python using different approaches.
Example 1
- Input: side = 6 units
- Output: 24 units
Explanation
Using the formula to calculate the perimeter of the square: 4 × side = 4 × 6 = 24 units
Example 2
- Input: side = 0 units
- Output: 0 units
Explanation
If the side length is zero, the square does not exist as a two-dimensional figure, and hence, there is no perimeter.
Example 3
- Input: side = 12 units
- Output: 48 units
Explanation
Using the formula to calculate the perimeter of the square: 4 × side = 4 × 12 = 48 units
Formula
The perimeter of a square is calculated using the formula:
Perimeter = 4 × side
Approaches to Calculate Perimeter
- Using Direct Formula
- Using Function
- Using Class
Using Direct Formula
We use the direct formula to calculate the perimeter of the square. The formula is Perimeter = 4 × side. This is the simplest approach ?
# Take the side length as input
side = 5
# Calculate the perimeter using the formula
perimeter = 4 * side
print(f"The perimeter of the square with side {side} is: {perimeter}")
The perimeter of the square with side 5 is: 20
Time Complexity: O(1)
Space Complexity: O(1)
Using Function
We use a function to calculate the perimeter of the square. This makes the code more modular and reusable ?
# Function to calculate the perimeter of a square
def calculate_perimeter(side):
return 4 * side
# Input: side length of the square
side = 6
# Call the function
perimeter = calculate_perimeter(side)
print(f"The perimeter of the square with side length {side} is: {perimeter}")
The perimeter of the square with side length 6 is: 24
Time Complexity: O(1)
Space Complexity: O(1)
Using Class
We can create a class to represent a square and include methods to calculate its perimeter. This approach is useful for object-oriented programming ?
class Square:
def __init__(self, side):
self.side = side
def calculate_perimeter(self):
return 4 * self.side
def display_info(self):
perimeter = self.calculate_perimeter()
print(f"Square with side {self.side} has perimeter: {perimeter}")
# Create square objects
square1 = Square(8)
square2 = Square(3.5)
# Display information
square1.display_info()
square2.display_info()
Square with side 8 has perimeter: 32 Square with side 3.5 has perimeter: 14.0
Comparison
| Method | Reusability | Best For |
|---|---|---|
| Direct Formula | Low | Simple one-time calculations |
| Function | High | Multiple calculations |
| Class | High | Object-oriented design |
Conclusion
The perimeter of a square can be calculated using the simple formula 4 × side. Use functions for reusability or classes for object-oriented approaches depending on your requirements.
