📚

Rectangle Area

Using Variables in Calculations

Variables become really powerful when you use them in math expressions. Instead of writing raw numbers, you can write formulas using variable names:

width = 8
height = 5
area = width * height
print(area)   # → 40

Let's trace through what Python does:

  1. Creates
    width
    and sets it to
    8
  2. Creates
    height
    and sets it to
    5
  3. Calculates
    width * height
    8 * 5
    40
  4. Stores the result
    40
    in a new variable called
    area
  5. Prints
    area
    → displays
    40

Why Use Variables Instead of Numbers?

You could just write

print(8 * 5)
and get the same result. But variables make your code:

  • Readable
    area = width * height
    is clearer than
    40
  • Reusable — change
    width
    once and everything updates
  • Meaningful — the names explain what the numbers represent
💡

💡 Math operators in Python:

+
(add),
-
(subtract),
*
(multiply),
/
(divide). You'll learn more in the Math lesson!

main.pySandbox
main.py