In this article, we will learn different ways to calculate the Area, Perimeter and Diagonal of a Rectangle using Python, with detailed explanations and examples.
Table of contents
What is Rectangle?
In simple terms, a rectangle is a flat, four-sided polygon shape where:
- All four internal angles are right angles (90 degrees). This is the defining characteristic.
- It has four straight sides.
- Opposite sides are parallel and equal in length.

Examples of Rectangles in Real Life are Mobile Phones, Computer Screens, Books, Doors, etc.
We define a rectangle using length and width.
- Length (L): The longer side of the rectangle.
- Width (W): The shorter side of the rectangle
Now, let’s understand Area, Perimeter, and Diagonal of a Rectangle.
Area of Rectangle
The area of a rectangle is the amount of space enclosed within its four sides.
Formula to Calculate the Area of a Rectangle
Area = Length × Width
Area (A): The total surface covered by the rectangle, measured in square units (e.g., cm², m², inches²).
For Example, If a rectangle has: Length = 10 meters, Width = 5 meters, then, the Area is: 10 × 5 = 50 m²
Perimeter of a Rectangle
The perimeter of a rectangle is the total length of its boundary. It is calculated by adding the lengths of all four sides.
Formula to Calculate the Perimeter of a Rectangle:
Perimeter = 2 × ( Length + Width )
For Example, Assume rectangle has: Length = 10 meters, Width = 5 meters, then perimeter is: P = 2 × ( 10 + 5 ) = 2 × 15 = 30 meters
Diagonal of a Rectangle
The diagonal of a rectangle is the straight-line distance between opposite corners (vertices) of the rectangle. It divides the rectangle into two right-angled triangles.
Formula to Calculate the Diagonal of a Rectangle:
The diagonal (d) of a rectangle can be found using the Pythagorean Theorem, since a rectangle forms a right triangle when the diagonal is drawn:
d = √(L² + W²)
- d = Diagonal
- L = Length of the rectangle
- W = Width of the rectangle
- √ = Square root function
For Example, If a rectangle has: Length = 10 meters, Width = 5 meters
1. How to Calculate Area, Perimeter and Diagonal of a Rectangle
Steps to find the Area of a Rectangle in Python:
- Identify the Dimensions
Length (L) – The longer side of the rectangle.
Width (W) – The shorter side of the rectangle. - Calculate the Area using the below formula
Area = Length × Width
- Calculate the Perimeter using the below formula
Perimeter = 2 × ( Length + Width ) - Calculate the Diagonal using the below formula
In Python,
diagonal = math.sqrt( length ** 2 + width ** 2 )
Here, the square of length and width is calculated using the Exponentiation operator (**) and added.
The square root is computed usingmath.sqrt()function. Themath.sqrt()function returns the square root of a given non-negative number. - Mention the Correct Unit
If the length and width are in meters, the Area will be in square meters (m²).
The 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 50 square units. Perimeter is 30 units. Diagonal is 11.180339887498949 units.
2. Calculate Using User Inputs
If you want to calculate area using the length and width specified by user then use the input() function.
The input() function allow us to get input from the user while a Python script is running. It pauses the execution of your program and waits for the user to type something into the console (or terminal) and press the Enter key.
The input() always returns the user’s input as a string, regardless of whether the user typed numbers, letters, or symbols. So you must convert it to numbers using the float() function.
Taking input from the user dynamically allows flexibility.
Code Example
Output:
Enter the length of the rectangle: 10
Enter the width of the rectangle: 5
Area is 50.0 square units. Perimeter is 30.0 units. Diagonal is 11.180339887498949 units.
3. Calculate Using a Function
There are multiple advantages of using a function to calculate the area of a rectangle as follows:
- Encapsulating the calculation in a function makes the code reusable i.e., we can use it multiple times without repeating code.
- A function makes the code cleaner and more readable, improving maintainability.
- 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 rectangles without repeating the code.
Code Example
4. Calculate 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 rectangles need to be handled efficiently.
The following are the advantages of using OOP to calculate the area, perimeter and diagonal of a rectangle:
- Reusability: If you need to calculate the area of multiple rectangles, you can create multiple objects.
- Extensibility: With OOP, we have easily extended the class to calculate the perimeter and diagonal without affecting the existing structure.
Code Example
Explanation
- Define a Class
- Define a class named
Rectangle. - The class will store the length and width of the rectangle.
- Define a class named
- Create a Constructor (
__init__method)- The constructor (
__init__) initializes the length and width 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 = Length * Width
- 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:
2 × ( Length + Width )
- 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(self.length**2 + self.width**2)
- Define a method
- Create an Object of the Class
- Use
rect = Rectangle(10, 5)to create a rectangle with:length = 10, width = 5
- Use
- Call the Method to get the Area, Perimeter and Diagonal
- Use
rect.calculate_area()to get the area. - Use
rect.calculate_perimeter()to get the perimeter. - Use
rect.calculate_to get the diagonal.diagonal()
- 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 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
lengthandwidthare the input parameters.- To calculate Area:
length * widthis the calculation expression.rectangle_areais the name to call the anonymous lambda function.
- To calculate Perimeter:
2 * (length + width)is the calculation expression.rectangle_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 a rectangle’s area, perimeter, and diagonal in Python is simple and efficient, with multiple approaches that 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