Labeled boxes that store things
Day 21 of 149
π Full deep-dive with code examples
The Labeled Box Analogy
Imagine labeled storage boxes:
- Box labeled "Age" contains: 25
- Box labeled "Name" contains: "Alice"
- Box labeled "IsLoggedIn" contains: true
You can:
- Look inside (read the value)
- Put something else in (update the value)
- Use what's inside somewhere else
Variables are labeled boxes for storing data in your program!
Why Variables?
Without variables, you'd have to remember everything:
- "The user typed 42, now calculate..."
- "Wait, what number was it again?"
Variables let you:
- Store values for later
- Give meaningful names
- Easily change values
How They Work
Creating a variable:
name = "Alice" (box "name" now holds "Alice")
age = 25 (box "age" now holds 25)
isStudent = true (box "isStudent" holds true)
Using them:
greeting = "Hello, " + name (uses what's in "name")
birthYear = currentYear - age (uses what's in "age")
Types of Data
Variables can hold:
- Numbers β an integer or decimal
- Text β "Hello World"
- True/False β true, false
- Lists β ["a", "b", "c"]
- And more!
Changing Variables
Variables can change (that's why they're called "variable"!):
score = 0 (start at 0)
score = 10 (now it's 10)
score = score + 5 (now it's 15)
The box "score" contained different values over time.
In One Sentence
Variables are named containers that store data so your program can remember, update, and use values throughout its execution.
π Enjoying these? Follow for daily ELI5 explanations!
Making complex tech concepts simple, one day at a time.
Top comments (0)