Python Program to Calculate the Area of a Rectangle

When I first started teaching Python to beginners, one of the most common questions I got was: How do I calculate the area of a rectangle

It’s a simple problem, but it’s also one of the best ways to understand how Python handles inputs, variables, and basic arithmetic.

In this tutorial, I’ll show you step by step how to write a Python program to calculate the area of a rectangle. I’ll cover multiple methods, from the most basic approach to using functions and even handling user input.

By the end, you’ll have a clear understanding of how to approach this problem in Python, and you’ll be able to use the same logic for other shapes as well.

Formula for the Area of a Rectangle

The formula to calculate the area of a rectangle is simple:

Area = Length × Width

All we need to do in Python is multiply the length and width values and display the result.

Method 1: Simple Python Program

Let’s start with the most basic example.

# Program to find the area of a rectangle

# Length and width values
length = 10
width = 5

# Calculate area
area = length * width

# Display result
print("The area of the rectangle is:", area)

Output:

The area of the rectangle is: 50

I executed the above example code and added the screenshot below.

write a program to find area of rectangle in python

This is the simplest way to calculate the area. I hard-coded the values of length and width here.

Method 2: Take User Input

In real-world scenarios, we don’t want to hard-code values. Instead, we ask the user to enter them.

# Program to find the area of a rectangle with user input

# Taking input from user
length = float(input("Enter the length of the rectangle in feet: "))
width = float(input("Enter the width of the rectangle in feet: "))

# Calculate area
area = length * width

# Display result
print("The area of the rectangle is:", area, "square feet")

If you enter:

Length = 20  
Width = 15  

The output will be:

The area of the rectangle is: 300.0 square feet

I executed the above example code and added the screenshot below.

area of rectangle in python

This is more practical, especially for everyday calculations like measuring a backyard or a room in your house.

Method 3: Use a Function in Python

I often recommend using functions because they make your code reusable and cleaner.

# Program to find the area of a rectangle using a function

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

# Example usage
length = 25
width = 10
print("The area of the rectangle is:", rectangle_area(length, width))

Output:

The area of the rectangle is: 250

I executed the above example code and added the screenshot below.

find the area of rectangle in python

With this method, you can call the rectangle_area function anytime with different values.

Method 4: Use Python Class

If you’re working on larger projects, using a class is a good option.

# Program to find the area of a rectangle using a class

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

# Example usage
rect = Rectangle(30, 12)
print("The area of the rectangle is:", rect.area())

Output:

The area of the rectangle is: 360

I executed the above example code and added the screenshot below.

python program to find area of rectangle

This approach is more structured and fits well if you’re learning object-oriented programming in Python.

Method 5: Area in Different Units

Sometimes, you may want the result in square meters instead of square feet. Here’s how you can handle that.

# Program to convert area from square feet to square meters

def rectangle_area_in_meters(length_feet, width_feet):
    # 1 square foot = 0.092903 square meters
    area_sqft = length_feet * width_feet
    area_sqm = area_sqft * 0.092903
    return area_sqm

length = 50
width = 40
print("The area in square meters is:", rectangle_area_in_meters(length, width))

Output:

The area in square meters is: 185.806

This can be really useful if you’re working on projects where you need metric units.

Method 6: Use Python Lambda Function

For quick calculations, I sometimes use a lambda function.

# Program to find the area of a rectangle using lambda

area = lambda length, width: length * width

print("The area of the rectangle is:", area(12, 8))

Output:

The area of the rectangle is: 96

This is a one-liner solution, perfect for small scripts.

Common Mistakes to Avoid

  • Forgetting to convert input values to float or int. By default, input() gives you a string.
  • Mixing up length and width doesn’t matter mathematically, but it’s good practice to keep them consistent.
  • Not considering units (feet, inches, meters). Always clarify what unit you’re working with.

Conclusion

Finding the area of a rectangle in Python is one of the simplest yet most practical problems you can solve as a beginner.

I showed you different methods, from basic multiplication to using functions, classes, and even unit conversion.

I recommend starting with the simple method and then moving on to functions and classes as you get more comfortable. That way, you’ll not only learn how to calculate areas but also improve your overall Python skills.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.