Python Program to Find Area of Rectangle
Last Updated :
22 Feb, 2025
The task of calculating the Area of a Rectangle in Python involves taking the length and width as input, applying the mathematical formula for the area of a rectangle, and displaying the result.
Area = Width * Height
Where:
- Length is the longer side of the rectangle.
- Width is the shorter side of the rectangle.
For example, if Length = 5 and Width = 8, the area is calculated as Area=5×8=40.
Using * operator
The direct multiplication approach is the most efficient way to find the area of a rectangle . It involves multiplying the length and width directly using the * operator.
Python
x = 5 # length
y = 8 # width
res = x * y
print(res)
Explanation: This code calculates the area of a rectangle using the formula length * width. Here, x = 5 represents the length and y = 8 represents the width. The multiplication x * y is stored in res, which holds the area value.
Using lamda function
A lambda function provides a quick and concise way to compute the area of a rectangle in a single line. It is often used in cases where you need an inline solution, especially when working with functional programming concepts like map() or reduce().
Python
x = 5
y = 8
area = (lambda length, width: length * width)(x, y)
print(area)
Explanation: It multiplies x = 5 and y = 8 in a single line area = (lambda length, width: length * width)(x, y) and the result 40, is stored in area.
Using numpy
NumPy is a powerful library for numerical computations, but using it to find the area of a single rectangle is overkill. It is more suitable when dealing with arrays of lengths and widths. This approach is only recommended when performing bulk calculations involving multiple rectangles.
Python
import numpy as np
x = np.array(5) # length
y = np.array(8) # width
area = np.multiply(x, y)
print(area)
Explanation np.multiply(x, y) performs element-wise multiplication of x and y using NumPy's multiply() function and stores the result (area) in the variable area.
Similar Reads
Python Program to Find all rectangles filled with 0 We have one 2D array, filled with zeros and ones. We have to find the starting point and ending point of all rectangles filled with 0. It is given that rectangles are separated and do not touch each other however they can touch the boundary of the array.A rectangle might contain only one element.Exa
4 min read
Python Program for Program to calculate area of a Tetrahedron A Tetrahedron is simply a pyramid with a triangular base. It is a solid object with four triangular faces, three on the sides or lateral faces, one on the bottom or the base and four vertices or corners. If the faces are all congruent equilateral triangles, then the tetrahedron is called regular. Th
2 min read
Python Program to find volume, surface area and space diagonal of a cuboid Given the length, base, and height of a cuboid. The task is to find the Surface Area, Volume and space diagonal of the cuboid. Examples: Input : length = 9 breadth = 6 height = 10 Output : Surface area = 408 volume = 540 space diagonal = 14.73 Input : length = 5 breadth = 4 height = 3 Output : surfa
2 min read
Calculate the Area of a Triangle - Python A triangle is a closed-shaped two-dimensional polygon having three sides and three corners. The corners are called vertices and the sides are called edges. In this article, we will see how we can calculate the area of a triangle in Python.Using Heron's FormulaWhen the lengths of all three sides of t
2 min read
Python Program to Check if right triangle possible from given area and hypotenuse A triangle is said to as having "right angles" if one of its angles is a right angle. The "legs" of a triangle are the two sides that cross over one another to make a right angle. The "hypotenuse" side of a triangle is the side that faces the right angle formed by the triangle. In this article, we'l
6 min read