Things that can change, and things that can't
🌍 In real life. A whiteboard can be rubbed out and rewritten: it's changeable. A printed certificate can't; to "fix" one, the office prints a new certificate and hands that over instead. If two people were holding copies of the old one, theirs stay exactly as printed.
Some Python objects are whiteboards, and some are certificates.
The idea in one sentence: mutable objects (lists, dictionaries, sets) can be changed in place; immutable ones (numbers, strings, tuples) can't, so "changing" them really makes a new object and moves the tag.
Its words:
- mutable: can change after it's made
- immutable: can never change; every "change" makes a new object
- rebinding: moving a name's arrow to a different object, as
=does
Step by step, for x = 5, y = x, y += 1:
xandyboth point at the number 5.y += 1can't change 5 (numbers are immutable), so Python makes a new number, 6.- Only
y's arrow moves to 6;xstill points at 5.
x = 5
y = x
y += 1
print(x, y)Strings work the same way. String methods like .upper() never change the string; they give back a new one.
s = "cat"
s.upper()
print(s)
s = s.upper()
print(s)Careful with lists: a += [2] changes the list itself (every name sees it), but a = a + [3] makes a new list and moves only a.
a = [1]
b = a
a += [2]
a = a + [3]
print(a)
print(b)Why it matters. With immutable values, sharing is always safe: nobody can change the 5 or the "cat" behind your back. With mutable values, every name pointing at the object sees every change made in place. So the question to ask is: "does this line change the object, or make a new one?" append, += on a list, and d[k] = v change the object; = and + make or move.
Mistakes and edge cases.
- Changing a tuple.
t[0] = 5givesTypeError: 'tuple' object does not support item assignment(see the bug question). - Calling a string method and not keeping it.
s.upper()makes a new string and throws it away. - += on a list vs on a number.
a += [2]changes the shared list;y += 1on a number moves onlyy. - A tuple holding a list. The tuple can't change, but the list inside it still can:
t = ([1],)thent[0].append(2)works.
Quiz
What does this print?
x = 1 y = x y = y + 1 print(x)What does this print?
s = "cat" s.upper() print(s)What does this print?
a = [1] b = a a += [2] print(b)What does this print?
a = [1] b = a a = a + [2] print(b)Which of these can be changed in place?
- A list
- A string
- A tuple
Which line stops this program with an error?
point = (1, 2) print(point[0]) point[0] = 5 print(point)
Coding tasks
Ask for a word. Make a capital version of it with
.upper()and print it, then print the word you were given, unchanged.Start with
scores = [3, 5]andbackup = scores. Makescorespoint to a new list with 7 added at the end, without changingbackup. Then printbackup, thenscores.Ask for some letters on one line, separated by spaces, and put them in a list. Reverse the list in place by swapping: the first with the last, the second with the second-to-last, and so on. Print the list.