Why does `L[i] = 5` change a list in place while reassigning a tuple silently builds a brand-new object?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Lists are mutable, so you can overwrite a single element without rebuilding the whole list. The list scores holds four numbers. Change the element at index 2 to 100, in place, so the list becomes [88, 91, 100, 64]. Keep the list bound to scores.
List operations carried over from strings and tuples
Write out the mixed-type list L = [2, 'a', 4, [1, 2]] and record length, indexing, slicing, concatenation, max, and a for loop over its elements.
L[i] = value as an in-place assignment
Trace L = [2, 4, 3] followed by L[1] = 5 on a memory diagram, showing the name L, the single object it points to, and the middle element overwritten to 5.
Rebinding a tuple name versus changing an object
Put t = (2, 4, 3) then t = (2, 5, 3) beside the list case, and draw the new tuple object together with the untouched original that lost its name.