Paul's Python CoursePPC

Two labels, one list

Not met yet

b = a doesn't copy a list. It puts a second label on the same list, so a change made through b shows up in a. For a separate list, use a.copy(), a[:] or sorted(a). Moving a label with a new = is different: a = [9] points a at a new list and leaves b where it was.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks b = a makes a separate copy of a list, so changing b leaves a alone.
  • Thinks b = a ties b to a, so changing a number in a later changes b too.

Where it comes up