Paul's Python CoursePPC

References

Not met yet

A variable doesn't hold a list or dictionary; it refers to one, like a label stuck on a box. b = a sticks a second label on the same box. Passing a list to a function hands over another label on it, so a function that changes the list changes the caller's list too.

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 a function receives its own copy of a list or dictionary, so changes inside can't reach the caller.
  • Thinks putting a list inside another list copies it, so later changes to the original don't show.

Where it comes up