Neural Sync Active
Variables and State
Registry Synced
Variables and State
152 words
1 min read
Variables and State
Variables are labels attached to values. The label can point at a new value later, which means a program has state that changes over time.
Core idea
An assignment like
score = score + 1 should be read in two phases:- Read the current value of
score. - Store the new value back under the same name.
Trace habit
Create a tiny table:
| Step | Code | State |
|---|---|---|
| 1 | score = 0 | score: 0 |
| 2 | score = score + 1 | score: 1 |
This table prevents a common beginner mistake: thinking both sides of
= update at the same time.Practice
Trace this mentally before running it:
pythonlevel = 2 level = level + 3 level = level * 2 print(level)
The final value is
10.