State is what an object contains right now. A grocery list, for example, has a different state after you add bread. In Python, a mutable object can change its contents without becoming a new object. Lists and dictionaries are mutable; strings, numbers, Booleans, and tuples are not. section 24.2 section 24.3 section 25.1
Change the same list
Start with `a = ["milk", "eggs"]`, then run `a.append("bread")`. The list’s state is now `["milk", "eggs", "bread"]`: `a` still refers to the same object. By contrast, assigning `a = ["milk", "eggs", "bread"]` binds `a` to a new list instead. Both leave you with the same visible items, but they do not do the same thing to the original object. section 24.2
Figure 24.3. On the left, you have a grocery list at a certain memory location. On the right, you add another item to your grocery list, and the object at the same memory location is directly modified.
The grocery-list diagram shows an item being added while the list remains at the same memory location. section 24.2
A list’s current state affects the next operation. For example, after `colors = ["red", "blue", "yellow"]`, the command `colors[0] = "orange"` replaces the first item. The list is now `["orange", "blue", "yellow"]`; later operations work with that changed list. You can also add, remove, reorder, or sort list items without reassigning the list variable. section 25.6 section 26.0
One important surprise: two variable names can refer to the same mutable object. If the object changes through either name, both names lead to its new state. To keep independent lists, make a copy—for example, `painters = artists.copy()`. Changing `painters` then leaves `artists` unchanged. Likewise, `sorted(ages)` produces a sorted copy, whereas `ages.sort()` changes the existing list. section 28.0 section 28.2
Quick check: if `L = [1, 2, 3]` and then `L[0] = 4`, what does `L` contain? It contains `[4, 2, 3]`. For a changing menu or grocery list, ask whether you want to update the existing collection or preserve its earlier state in a separate copy. section 25.6 section 25.7 section 28.2






































