2022 Teaching Notes

In the lesson we need to introduce the difference between how variables store by reference and how they store by value. Something like the following

By value

a and b each get their own copy of the value. If you assign b = a, then b gets its own copy of the value stored in a. Anything you do later on to a won’t have any effect on b.

>>> a = "alligator"
>>> b = a
>>> a = "crocodile"
>>> a
"crocodile"
>>> b
"alligator"

By reference

In contrast, lists, dictionaries, and class instances are stored by reference. If you assign b = a, you’re setting b to point at the same object which a points to. There’s only one object; if you modify a, b will also be changed becuase they’re both names for the same object.

>>> a = []
>>> b = a
>>> a.append("nectarine")
>>> b
["nectarine"]

Tuples

Now you are ready to understand a data structure called a tuple, which is like a list but written with parentheses instead of brackets, like (1, 2, 3). A tuple is stored by value. It’s not a class instance and doesn’t have any methods. You can’t append to a tuple or change it. Later on, you’ll learn when and wny tuples are useful.