In this article, we will explore different ways to calculate the Area, Perimeter and Diagonal of a Square using Python, with detailed explanations and examples.
Table of contents
What is a Square
A square is a four-sided polygon (quadrilateral) where:
- All four sides are equal in length.
- All four angles are 90 degrees (right angles).
- The opposite sides are parallel to each other.
- Both diagonals are equal in length and bisect each other at 90 degrees.

Examples of Squares in Real Life are Chessboard, Tiles on a floor, etc.
Understand the Area of a Square
The area of a square is the amount of space enclosed within its four equal sides. It is measured in square units such as square centimeters (cm²), square meters (m²), or square inches (in²).
Formula to Calculate the Area of a Square:
Area = Side²
- Side (S) = Length of one side of the square
- Area is always expressed in square units (e.g., cm², m², in²).
For Example,
- If a square has a side length of 5 meters: Area = 5² = 25 m²
- If a square has a side length of 4.5 cm: Area = 4.5² = 20.25 cm²
Understand the Perimeter of a Square
The perimeter of a square is the total length of its boundary. It is the sum of all four equal sides of the square.
The formula for the Perimeter of a Square:
Perimeter = 4 × Side
- Side (S) = The length of one side of the square.
- Perimeter (P) = The total length around the square.
For Example, If a square has a side length of 6 meters:
Then, the perimeter is: Perimeter = 4 × 6 = 24 meters
Understand the Diagonal of a Square
The diagonal of a square is the straight line connecting two opposite corners (vertices) of the square. Every square has two diagonals, and they are:
- Equal in length
- Perpendicular to each other
- Bisect each other at 90°
Diagonal Formula:
Diagonal = √2 × Side
Where:
- Side = the length of one side of the square
- √2 ≈ 1.4142
For Example, If the side of a square is 4 cm, then:Diagonal = √2 × 4 ≈ 1.4142 × 4 = 5.6568 cm
1. How to Calculate Area, Perimeter and Diagonal of a Square
Below are the steps to find the Area, Perimeter and Diagonal of a Square in Python:
- Identify the Side Length
A square has four equal sides.
- Apply Area Formula
The formula to calculate the area of a square is:
Area = Side²
If Side = 5 meters, then: Area = 5² = 5 × 5 = 25 m² - Apply the Perimeter Formula
The formula to calculate the Perimeter of a Square is:
perimeter = 4 * side
If side = 10 meters, then:perimeter= 4 × ( 10 ) = 40 meters - Apply Diagonal Formula
The formula to calculate the diagonal of a square is:
diagonal = √2 × Side
In Python,diagonal = math.sqrt(2) * side
The square root of 2 is calculated usingmath.sqrt()function.
Themath.sqrt()function returns the square root of a given non-negative number. - Mention the Correct Unit
If the side length is in meters (m), → Area is in square meters (m²), Perimeter and Diagonal are in meters (m).
Likewise for other units like cm, inch, etc. - Display the result
Display the Area, Perimeter and Diagonal using the
print()function
Code Example
Output:
Area is 25 square units, Perimeter is 20 units, Diagonal is 7.0710678118654755 units
2. Calculate Using User Inputs
If you want to calculate area using the value specified by user then use the input() function. As the input is in string format, we must convert it into a float for the calculations.
Taking input from the user dynamically allows flexibility.
Code Example
Output:
Enter the side length of the square: 5
Area is 25.0 square units, Perimeter is 20.0 units, Diagonal is 7.0710678118654755 units
3. Calculate Using a Function
There are multiple advantages of using a function to calculate the area, perimeter and diagonal of a square as follows:
- Encapsulating the calculation in a function makes the code reusable and modular.
- A function allows us to write the calculation once and use it multiple times without repeating code.
- If a program requires multiple area calculations, a function makes it scalable.
- Avoid errors: A function helps prevent mistakes by keeping the formula in one place.
- Easier Debugging & Updating: If the formula needs to be updated or changed, a function makes it easy to modify in one place.
In the example below, we have different functions to calculate area, perimeter, and diagonal, which can be reused for multiple squares without repeating the code.
Code Example
4. Calculate Area of Square Using Class (OOP)
Object-Oriented Programming (OOP) is a structured way to write code by creating classes and objects. A class is a blueprint for creating objects, while an object is an instance of a class.
This approach provides better structure, reusability, and maintainability in your code. This approach is highly useful, especially for larger applications where multiple squares need to be handled efficiently.
The following are the advantages of using OOP to calculate the area, perimeter and diagonal of a square:
- Reusability: If you need to calculate the area of multiple squares, you can create multiple objects.
- Extensibility: With OOP, we can easily extend the class to calculate the perimeter and diagonal without affecting the existing structure.
Code Example
Explanation
- Define a Class
- Define a class named
Square. - The class will store the side of the square.
- Define a class named
- Create a Constructor (
__init__method)- The constructor (
__init__) initializes the side when an object is created. - The
selfkeyword refers to the current instance of the class.
- The constructor (
- Create a Method to Calculate Area
- Define a method
calculate_area()inside the class. - This method returns the area using the formula:
Area = Side²
- Define a method
- Create a Method to Calculate Perimeter
- Define a method
calculate_perimeter()inside the class. - This method returns the perimeter using the formula:
4 × ( side )
- Define a method
- Create a Method to Calculate Diagonal
- Define a method
calculate_diagonal()inside the class. - This method returns the diagonal using the formula:
d = math.sqrt(2) * side
- Define a method
- Create an Object of the Class
- Use
sq = Square(5)to create a square with:side = 5
- Use
- Call the Methods to get the Area, Perimeter and Diagonal
- Use
sq.calculate_area()to get the area of the object. - Use
sq.calculate_perimeter()to get the perimeter of the object. - Use
sq.calculate_diagonal()to get the diagonal of the object.
- Use
- Store the result in a variable and display it using
print().
5. Calculate Using Lambda
A lambda function in Python is a small, anonymous function that can have multiple arguments but only one expression, which is evaluated and returned. For a more compact implementation, you can use a lambda function.
A lambda function syntax: lambda arguments: expression
lambda→ Keyword to define the function.arguments→ Input parameters (like a normal function).expression→ A single operation that returns a result.
Code Example
Explanation
sideis the input parameter.- To calculate Area:
side ** 2is the calculation expression.square_areais the name to call the anonymous lambda function.
- To calculate Perimeter:
4 * sideis the calculation expression.square_perimeteris the name to call the anonymous lambda function.
- To calculate Diagonal:
math.sqrt(length**2 + width**2)is the calculation expression.rectangle_diagonalis the name to call the anonymous lambda function.
Summary
Calculating the area, perimeter and diagonal of a square in Python is simple and efficient, with multiple approaches to suit different programming needs. Whether you’re using basic arithmetic, user input, functions, object-oriented programming, or lambda functions, each method effectively applies the core formula.
Choose the approach that best fits your task—whether it’s a quick calculation or part of a larger application.

Leave a Reply