Paul's Python CoursePPC

Mutable and immutable

Not met yet

Lists, dictionaries and sets are mutable: they can be changed in place, and every reference to them sees the change. Numbers, strings and tuples are immutable: an operation that looks like a change makes a new value and moves the label. A tuple can't be changed, but a list inside it still can.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks b = a ties b to a, so changing a number in a later changes b too.
  • Thinks += on a number changes the number itself, so every name for it sees the change.
  • Thinks a list inside a tuple can't be changed because the tuple is immutable.
  • Thinks a string method like upper() changes the string in place.

Where it comes up