Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
Some Python collection types - strings and lists so far - are able to change and some are not. If a type is able to change, then it is said to be mutable. If the type is not able to change then it is said to be immutable. This will be expanded below.
9.2.1. Lists are Mutable¶
Unlike strings, lists are mutable. This means we can change an item in a list by accessing it directly as part of the assignment statement. Using the indexing operator (square brackets) on the left side of an assignment, we can update one of the list items.
An assignment to an element of a list is called item assignment. Item assignment does not work for strings. Recall that strings are immutable.
Here is the same example in codelens so that you can step through the statements and see the changes to the list elements.
By combining assignment with the slice operator we can update several elements at once.
We can also remove elements from a list by assigning the empty list to them.
We can even insert elements into a list by squeezing them into an empty slice at the desired location.
9.2.2. Strings are Immutable¶
One final thing that makes strings different from some other Python collection types is that
you are not allowed to modify the individual characters in the collection. It is tempting to use
 operator on the left side of an assignment, with the intention of changing a character
in a string. For example, in the following code, we would like to change the first letter of
Instead of producing the output
Jello, world!, this code produces the
TypeError: 'str' object does not support item assignment.
Strings are immutable, which means you cannot change an existing string. The best you can do is create a new string that is a variation on the original.
The solution here is to concatenate a new first letter onto a slice of
This operation has no effect on the original string.
While it’s possible to make up new variable names each time we make changes to existing values, it could become difficult to keep track of them all.
The more that you change the string, the more difficult it is to come up with a new variable to use. It’s perfectly acceptable to re-assign the value to the same variable name in this case.
9.2.3. Tuples are Immutable¶
As with strings, if we try to use item assignment to modify one of the elements of a tuple, we get an error. In fact, that’s the key difference between lists and tuples: tuples are like immutable lists. None of the operations on lists that mutate them are available for tuples. Once a tuple is created, it can’t be changed.
julia = 'X' # TypeError: 'tuple' object does not support item assignment
Check your understanding