Variables and Assignment in Text Code
Storing a value under a name in written code, then replacing it later. The name holds whatever was last assigned to it, which is how a program remembers anything.
What a learner can do afterwards
- Assign a value to a name and print it back out
- Predict what a name holds after two assignments in a row
- Pick names that say what the value means instead of single letters
1 · Read
A variable is a named box holding one value. You create it with an assignment: the name on the left, the value on the right. In text code you write score = 0, and from then on score means 0.
Programs remember by reassigning. Start with score = 0, then later write score = 5 after a goal. The same name now holds 5, and the old 0 is gone for good. To show a value, print it back out.
Values come in kinds. Text sits in quotes, like name = "Lia", while numbers stand bare, like age = 14. True and false stay lowercase with no quotes. Each kind behaves differently when your program uses it.
Pick names that say what the value means. Player score beats x every time, because future you will read this code. Short single letters hide meaning, while clear lowercase names reveal it.
Store a value under a clear name, print it to show it, and reassign to change it.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.