Calculating Area in Python: Variables and Multiplication in Action
The area of a rectangle exercise is deceptively simple — but it's one of the most important exercises for a beginner. It forces you to practice three essential skills at once: storing values in variables, applying an arithmetic operator to those variables, and printing the result. These same three steps appear in virtually every real-world calculation you'll ever write.
The Formula
The area of a rectangle is: area = width × height
In Python, multiplication uses the * operator:
width = 7
height = 3
area = width * height
print("Area:", area) # Area: 21
Step-by-Step Breakdown
- Line 1–2: Store the rectangle's dimensions in variables. Using descriptive names (
width,height) makes the code self-explanatory. - Line 4: Calculate the area by multiplying the two variables. The result (an integer or float) is stored in
area. - Line 6: Print the result. Python evaluates
areato its stored value (21) at this point.
Working With Different Data Types
Width and height can be integers or floats. Python handles both automatically:
# Integer dimensions:
width = 5
height = 8
print(width * height) # 40
# Float dimensions:
width = 3.5
height = 2.0
print(width * height) # 7.0
# Mixed (int and float):
width = 4
height = 2.5
print(width * height) # 10.0 (Python promotes to float)
Putting It in a Function
For reusable code, wrap the logic in a function — this is how real programs are structured:
def rectangle_area(width, height):
return width * height
# Calculate areas for different rectangles:
print(rectangle_area(5, 10)) # 50
print(rectangle_area(3, 7)) # 21
print(rectangle_area(2.5, 4)) # 10.0
# Use it in context:
room_width = 4.8 # meters
room_height = 6.2
area = rectangle_area(room_width, room_height)
print(f"Room area: {area:.1f} m²") # Room area: 29.8 m²
Extending the Pattern: Other Shapes
The same variable-and-formula pattern works for any geometry:
# Triangle: base * height / 2
base = 6
height = 4
triangle_area = base * height / 2
print(triangle_area) # 12.0
# Circle: π * r²
import math
radius = 5
circle_area = math.pi * radius ** 2
print(round(circle_area, 2)) # 78.54
Common Mistakes to Avoid
- Adding instead of multiplying:
width + heightgives the wrong answer — the formula requires*. - Using variables before assigning them: Always assign
widthandheightbefore computingarea. - Forgetting to print: In Python, calculations don't display unless you explicitly print them.
Key Takeaways
- Store values in clearly named variables; use them in formulas with arithmetic operators.
- Area of a rectangle:
area = width * height. - Python works with both integers and floats — mix them freely.
- Functions make calculations reusable — wrap any formula in a function when you'll call it more than once.
What to Learn Next
This exercise combines variables and multiplication — two skills you've now practiced together. Next, explore functions with parameters to make your calculations reusable across a whole program, and try computing areas of other shapes like circles and triangles to reinforce the pattern.
