Basic

A name is a handle for a value

A value that is only printed exists for one instant and then disappears. To keep a value you give it a name. The name is not the value, it is a handle you can reach for again, and that difference is the whole point of assignment.

What you will see

NameError

About 2 min of reading

Try It

Store one value under a name and reuse it.

1

Store the number 10 under the name score.

score = 10

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

What it prints
>>>
2

Print the name. The computer swaps in the value you stored.

print(score)

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

What it prints
10
3

Use the name in a sum. It behaves exactly like the number it holds.

print(score + 5)

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

What it prints
15
4

Try to use a name you never stored. The computer has nothing to hand back and complains.

print(price)

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

What it prints
NameError
A name must be stored before it is used. This error is the computer telling you the handle was never filled, which is a common first bug rather than a mystery.

Data Flow

You write a name and a value, the computer stores the value under the name, and later uses of the name hand that value back.
1 You write score = 10
↓
2 The value 10 is stored
↓
3 You write score again
↓
4 The computer hands back 10
Why this works
Choosing a name

The computer does not care whether you write x or playerScore, but you do. A name that says what the value means makes the program readable a month later. Short names are fine for throwaway counters, and bad for anything that carries meaning.

When you have a choice

Name versus value

A name is a label you chose, the value is the thing it points at. Two different names can point at the same value, and one name can be made to point at a new value later. Holding that split in mind prevents a whole class of confusion where you think changing a name changes the value everywhere.

Examples

The arrow is not equals

score = 10

Read this as "put the value 10 into the name score". The = sign in code is an instruction to store, not a claim that two sides are equal. The left side must be a name, the right side is a value.

Reach for the name

score = 10
print(score)

The name stands in for the value from that line onward. Wherever you write score, the computer swaps in 10. You can use the name as many times as you like and it always hands back the same value.

Names save you the work

total = 2 + 2
print(total)
print(total * 10)

The sum is worked out once and stored. The two prints reuse the answer instead of computing it again. For a small sum that saves nothing, but the pattern is how a real program keeps a result.

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 value is something the machine holds
Previous Lesson
A value is something the machine holds
Next Lesson
A name can be filled again
A name can be filled again →