Write a Program to Find the Area of a Rectangle in Python

In this Python tutorial, I teach you how to write a program to find the area of a rectangle in Python.

While building the advanced calculator app in Python, I had to add a common functionality, such as computing the area of a rectangle based on the given length and width, where I implemented the functionality using a function.

But I thought I would share this information with you. You may be required to implement the same functionality at some point in your program.

So, in this tutorial, I have explained three different methods for finding the area of a rectangle using a simple program, a function and a lambda function.

Let’s begin,

Python Program to Find the Area of a Rectangle

To write a program to find the area of a rectangle in Python, first, you need to know the formula to calculate the area of a rectangle; from my childhood mathematics classes, I remembered that the formula is area = length x breadth.

So, you need to multiply the rectangle’s length or breadth (width) to compute its area. Let’s check with a simple example.

Suppose you need to install new flooring in your rectangular room and need to know how much material to buy. For that, you will need to compute its area, and the length and width of the room are 10 meters long (length) and 12 meters wide (breadth or width).

So, in Python, first create and initialize the two variables, length and width, with values 10 and 12, respectively, as shown below.

length = 10
width = 12

To compute the area, multiply these variables using the rectangle formula below.

room_area = length * width

Print the computed area of the room using the code below.

print('The area of rectangular room in meters is :', room_area)
Python Program to Find Area of Rectangle

In the above code, when length and width are multiplied by each other, the result is 120, which is the area of the rectangular room.

READ:  Python Pretty Print JSON

But in the above program, to compute the area of the rectangle in Python, you have supplied the length and width of the rectangle in a hard-coded way, which means by initializing variable values.

So, that is not good Python code for computing the area of a rectangle. Let’s create another good one. To calculate the area of the rectangle, you will have to provide only the length and width of the rectangle to that program.

Python Program for Area of Rectangle using Function

Create a function named area_of_rectangle, as shown below.

def area_of_rectangle(length, width):
  return length * width

The above program is called a function (area_of_rectangle). It takes two values: length and width. Then, the function returns the area of a rectangle, so here, the return is the keyword to return something after performing some task; in this case, it is followed by the length * width.

You have created a Python program named area_of_rectangle(length, width). that takes the length and width of any rectangle and returns the area of that rectangle.

Let’s pass the rectangle’s length and width as 20 and 15, respectively, as shown in the code below.

rectangle_area = area_of_rectangle(20, 15)

print('The area of rectangle is :',rectangle_area)
Python Program for Area of Rectangle

Look at the above code. you just called the function area_of_reactangle(20, 15) with length and width equal to 20 and 15, and it returned the area of a rectangle.

That’s an easy program; every time, you need to provide the length and width of the rectangle to a function to calculate its area, and you don’t need to write the whole program again.

READ:  What are the Different ways to Create Strings in Python

This means you can use or call this program as many times as you want, with different lengths and widths.

So, you created a Python program for a rectangle of area using the concept of function. Now, I want to show one more way to write a Python program to find an area of a rectangle: by using the lambda function.

Area of Rectangle in Python using Lambda Function

The lambda function in Python is also called the anonymous function (a function without a name); it can accept any number of arguments but can have only one expression.

But here, we have only two arguments: the rectangle’s length and width and a single expression, length * width.

Use the code below in Python to calculate the area of the rectangle.

area = lambda length, width : length * width

print('The area of rectangle is:',area(50, 70))
Area of Rectangle in Python using Lambda Function

So, in the above code, two arguments are passed to the lambda function using ‘lambda length, width’, then after the colon (:) an expression which is length * width for calculating the area of a rectangle.

As a result, it returns the area of the rectangle, which is 3500. So, this is how to find the area of the rectangle in Python using the lambda function.

You learned how to write a Python program to find a rectangle’s area using the above three methods.

Conclusion

In this Python tutorial, you learned to write a program to find the area of a rectangle in Python.

You learned how to write Python programs to find the area of rectangles using the function and lambda function.

You may like to read: