Variables: named storage locations
Be able to use a variable for scores or counters in a program.
Prerequisites
- BConditions in code: if–elserequired
Everyday explanation
A variable is a named location where the program stores a value. poäng = 0 creates a variable called poäng with the value 0. poäng = poäng + 1 retrieves the current value, adds 1, and saves the result back.
Without variables, a system cannot remember status, counts, or progress over time.
Interactive
Follow the variable:
liv = 3
liv = liv − 1 # 2
liv = liv − 1 # 1
liv = liv + 5 # 6
When you assign a new value, the old one is replaced. The variable has exactly one value at any given moment.
The name is not critical for the computer, but it is crucial for readability: poäng is clearer than p when you review the code later.
Mastery means
- Uses a variable as a counter in a program
- Tracks how a variable's value changes step by step
Sign in to do the exercises and build your mastery up.
Sources
- Scratch Wiki (CC BY-SA 4.0) — CC BY-SA 4.0
- The Python documentation (PSF licence) — PSF