Basic

A name can be filled again

Assignment is an instruction, not a permanent fact. The same name can be filled with a new value, and from that line on it points at the new one. This is how a program tracks change, a score, a step count, a turn.

What you will see

25

About 2 min of reading

Try It

Refill one name and watch the old value disappear.

1

Start with score at 10.

score = 10

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
2

Refill it with 20. The 10 is no longer held by anything.

score = 20

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
3

The common increase line. Read the right side first, then refill.

score = score + 5 print(score)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
25
4

Copy a value into a second name, then change the first. The second keeps its own copy.

b = score score = 0 print(b)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
25
b and score are separate handles. Re-filling score never touches b, which is why copying a value out is how you protect it from later change.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

The right side is worked out from the current values, then the name on the left is refilled with the result.
1 score holds 10
↓
2 score + 5 makes 15
↓
3 score is refilled with 15
↓
4 print shows 15
Why this works
Change over time

A program is a sequence of moments where names hold values. Reading a program means asking what each name holds at the line you are looking at. Re-assignment is the only way a value changes, so tracking it is how you follow the whole story.

When you have a choice

Assignment versus maths

In maths, x = x + 5 is impossible, nothing equals itself plus five. In code both sides are read differently: the right side is worked out first, then the left name is refilled. That order is why the line means "increase by five".

Examples

The name moves

score = 10
score = 20
print(score)

The second line replaces what score points at. The 10 is gone, nothing points at it any more. The print shows 20 because that is what score holds now.

The old value is already read

score = 10
score = score + 5
print(score)

The right side runs first. It reads the old 10, makes 15, and only then does score point at 15. The name on the left is being refilled, not compared.

Two names, one value

a = 5
b = a
a = 9
print(b)

b took the value 5 when it ran. Later changing a does not reach back into b, which still holds 5. Assignment copies the value at that moment, it does not tie the two names together.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A name is a handle for a value
Previous Lesson
A name is a handle for a value
Next Lesson
Values come in kinds
Values come in kinds →