This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 6: Variables

Variable assignment

face Josiah Wang

Try to guess what the ??? would be in the following example interactive prompt.

Assume that you are running each statement one after another. So your previous statements will affect the subsequent statements.

Try to make a guess first, then test it out yourself in an interactive prompt to confirm your answer.

Make sure you understand what is happening before moving on. I will not be giving the answers to this!

>>> x = 10
>>> print(x)
???
>>> print(type(x))
???
>>> y = 7.11
>>> print(y)
???
>>> print(type(y))
???
>>> x = "I am no longer the number 10"
>>> print(x)
???
>>> print(type(x))
???
>>> x = y
>>> print(x)
???
>>> print(type(x))
???
>>> y = "Imperial College London"
>>> print(y)
???
>>> print(type(y))
???
>>> print(x)
???
>>> print(type(x))
???

A variable does not have a type

Here is a subtle point: a Python variable itself does not have a type. It is just a nickname. It is the object that it is pointing to that has a type.

When you say type(y), you are really saying “what is the type of the object that y is referring to?” And not “what is the type of y?”