Skip to content
BIASED

Values, types and names

Block 1 of 8~2 min

A name is not a box

The topic Running Python ran a first script, samples.py. Its lines did two kinds of things: they created values, such as 32 and 4, and gave them names, such as batch_size and steps. The topic also left a loose end. The arguments in sys.argv arrive as text, '10' and not the number 10. This topic asks what a value and a name really are, and on the way it explains why '10' and 10 are different things.

The usual first picture comes from school algebra: a variable is a box with a label. x = 3 puts 3 into the box x. y = x copies the 3 into a second box y, and changing y leaves x alone. For numbers this picture gives the right answers. For a list it does not:

a = [1, 2]
b = a
b.append(3)
print(a)
[1, 2, 3]

b.append(3) adds 3 to the end of the list named b, and the list named a changed too. If b = a had copied the list into a new box, a would still be [1, 2]. So the box picture is wrong. The right picture has to explain this output, and also why numbers seemed to behave like boxes.

Answers are saved on the server and tied to this browser until you sign in.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • An expression for a discounted purchaseoptional

    Write one line and get a static check. Code is not executed; you can try it on a phone.

    ~5 min · any device · Read the block about values and arithmetic operations. The function wrapper is provided.

    Open practice