Registry Synced

Variables and State

152 words
1 min read

Reading compass

Now · Core idea

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:
StepCodeState
1score = 0score: 0
2score = score + 1score: 1
This table prevents a common beginner mistake: thinking both sides of = update at the same time.

Practice

Trace this mentally before running it:
python
level = 2
level = level + 3
level = level * 2
print(level)
The final value is 10.
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.