Zed-King Institute

    Python Program to Find Area and Perimeter of a Rectangle

    easyInput and operatorsPython

    A classic formula program: read two measurements, apply two formulas, print two answers on separate lines.

    The question

    Read the length and the breadth of a rectangle (integers, one per line). Print its area on one line and its perimeter on the next.

    The code

    program.py

    l = int(input())
    b = int(input())
    print("Area =", l * b)
    print("Perimeter =", 2 * (l + b))

    Input

    30
    3

    Output

    Area = 90
    Perimeter = 66

    How it works

    1. 1Read the length l and breadth b as integers.
    2. 2Area is l × b, written l * b in Python.
    3. 3Perimeter is 2 × (l + b) — the brackets make Python add before it multiplies.
    4. 4Each print() starts a new line, so area and perimeter appear on separate lines.

    Common mistakes

    • Writing 2 * l + b: without brackets only l is doubled, and the perimeter is wrong.
    • Using x for multiplication — Python only understands *.

    With decimal length and breadth

    int() fails on 4.5. Use float() when the sides can have a decimal point; the answers then print as decimals too.

    program.py

    l = float(input())
    b = float(input())
    print("Area =", l * b)
    print("Perimeter =", 2 * (l + b))

    Input

    4.5
    2

    Output

    Area = 9.0
    Perimeter = 13.0

    Using a function that returns both values

    A Python function can return two values at once, separated by a comma. They are unpacked into two variables on the calling line.

    program.py

    def rectangle(l, b):
        return l * b, 2 * (l + b)
    
    area, perimeter = rectangle(12, 5)
    print("Area =", area)
    print("Perimeter =", perimeter)

    Input

    (no input)

    Output

    Area = 60
    Perimeter = 34

    Questions students ask

    How do you find the area of a rectangle in Python?
    Multiply the length by the breadth: area = l * b. Read both values with int(input()) or float(input()) first, because input() gives text.
    What is the formula for the perimeter of a rectangle in Python?
    perimeter = 2 * (l + b). The brackets are required — without them Python doubles only the length.

    Now solve a similar question yourself

    A new Python question every time, checked instantly.

    Practice questions

    Preparing for NIELIT O Level?

    This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.

    More in Input and operators