📚
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:
- ◆Creates and sets it to
width8 - ◆Creates and sets it to
height5 - ◆Calculates →
width * height→8 * 540 - ◆Stores the result in a new variable called
40area - ◆Prints → displays
area40
Why Use Variables Instead of Numbers?
You could just write
print(8 * 5)- ◆Readable — is clearer than
area = width * height40 - ◆Reusable — change once and everything updates
width - ◆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
Ctrl + Enter