Open In App

Python Program to Find Area of Rectangle

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to find the area of a rectangle in Python if the length and width of a rectangle are given.

Formula of the Area of Rectangle

Area = Width * Height

Program To Find Area of Rectangle in Python

Below are some of the ways by which we can find the area of the rectangle in Python:

  • Simple Multiplication
  • Using a Class
  • Using a Lambda Function
  • Using a Named Tuple

Program to find the Area of a Rectangle by Simple Multiplication

In this method, we define a function that takes the length and width of a rectangle as parameters and calculates the area by simply multiplying them.

Python3




# Function to calculate the area using simple multiplication
def area_simple_multiplication(length, width):
    return length * width
 
# Example usage:
length = 5
width = 8
result = area_simple_multiplication(length, width)
print(f"Area of Rectangle: {result}")


Output

Area of Rectangle: 40


Program to find the Area of a Rectangle Using a Lambda Function

In this approach, we use lambda() function, to calculate the area by multiplying the given length and width. Lambda functions are concise, anonymous functions that can be handy for short-lived operations.

Python3




from functools import reduce
 
# Lambda function to calculate the area
def area_lambda(length, width):
    return reduce(lambda x, y: x * y, [length, width])
 
# Example usage:
length = 5
width = 8
result = area_lambda(length, width)
print(f"Area of Rectangle: {result}")


Output

Area of Rectangle: 40


Python Program to find the Area of a Rectangle Using a Class

In this method, a class is defined to represent a rectangle. The class has an __init__ method to initialize the length and width, and a method to compute the area using the stored values. This object-oriented approach encapsulates the rectangle-related functionality.

Python3




# Class to represent a rectangle and calculate its area
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width
 
    def calculate_area(self):
        return self.length * self.width
 
# Example usage:
rectangle_instance = Rectangle(5, 8)
result = rectangle_instance.calculate_area()
print(f"Area of Rectangle: {result}")


Output

Area of Rectangle: 40


Conclusion

In this article we studied different methods to calculate the area of a rectangle in Python, each employing distinct approaches and programming paradigms. The choice of method depends on factors such as code readability, project requirements, and the desired level of abstraction. Whether opting for simplicity, object-oriented design, functional programming, or a combination, Python offers the flexibility to choose the method that best suits the specific needs of the application.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads