Hone

Lessons · Python · storing a value in a name

Storing a value in a name

A name is a label you stick on a value so you can use it again later.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Every program remembers things. A shopping total, the user who is logged in, how many attempts are left. All of it is values with names on them, and this is that idea in its smallest form.

How to think about it

Ask: what does this program need to remember, and what should I call it so the next person knows what it holds?

Worked example

age = 30
Put the value 30 into a box and label the box age.
print(age)
Ask for whatever is in the box labeled age. You get 30.
age = 31
Stick the label on a different value. The old one is gone. A name holds exactly one thing at a time.

Your turn

Store your own name, then print it.

name = 
print(name)

The trap

age = 31 does not change 30 into 31 everywhere. It only moves the label. Anything that already copied the old value still has 30.

Practise storing a value in a name on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.