This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Variables

In Python, a variable is simply a name or a label that point to an object.

A variable points to an object

>>> x = 5
>>> print(x)
>>> print(type(x))
>>> y = complex(3, 5)
>>> print(y)
>>> print(type(y))

You can have different variables pointing to the same object.

Multiple variables can point to an object

Use id() to get the address of the object in memory. If two variables have the same id, they both point to the same object.

>>> a = "I love to learn"
>>> print(a)
>>> print(type(a))
>>> b = a
>>> print(b)
>>> print(type(b))
>>> print(id(a))
>>> print(id(b))
>>> print(id("I love to learn"))

You can also reassign an existing variable to point to a different object (of any type) at any stage of your program.

Variables can be reassigned to a different object

Try out the following, and make sure you understand what is happening.

>>> 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))

How am I allowed to name my variables?

A variable name in Python:

  • Can consist of letters, digits, or underscores (_)
  • Must start with a letter or an underscore
  • Cannot be a reserved keyword (e.g. True or while)

Can you figure out which variable names below are valid? Test them out to confirm (what error message would you receive?)

>>> my_variable123 = 2
>>> _12is_thisvalid = 2
>>> 123abc = 2
>>> try = 2
>>> __twounderscores = 2
>>> 1 = 2

Variable names are case-sensitive, so myvar is not the same as MyVar or MYVAR

>>> university = "London"
>>> University = "Imperial"
>>> print(university)
>>> print(University)

How should I name my variables?

Of course, what you are allowed do is different from what you should do.

Just because a variable name is valid does not mean that it is a sensible name.

  • Use semantically meaningful names

    x = 25 does not tell you much.

    age = 25 is self-explanatory.

  • Never use l, O, or I alone as a single character variable name. You yourself may have found that confusing to read. Was that a 0 or an O? Or is that a 1 or an l or an I?

  • Ending your variable names with an l or 1 might also be confusing. Is that an ideal or an idea1? Use with caution.

  • Never use Python built-ins (some of these are not keywords) as variable names (e.g. str, list). You can do it, but you will confuse yourself, others, and the Python interpreter, and suffer the consequences later.
    >>> x = str()
    >>> type(x)
    >>> str = 3
    >>> type(str)         # str is an int. Say that again?
    >>> str()             # the str type is gone!
    >>> str.capitalize()  # this is not a string!
    
  • The official style guide to Python recommends your variable names to be “lowercase, with words separated by underscores as necessary to improve readability… mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.” So they are essentially happy with something like height or my_beautiful_home, but do not recommend myBeautifulHome.

    Josiah says: “I am not overly strict about this. I myself keep changing my mind about whether to use lowercase_with_underscores or mixedCase. But try to use lowercase_with_underscores especially in a collaborative project.”