Example 1 — Read and change items by index
Show the steps and the answer
Number the positions from 0: apple is 0, bread is 1, milk is 2, eggs is 3.
An index counts steps from the start, so the first item is 0 steps away.
Count the items: len(cart) = 4.
Length counts items starting from 1, so it is always one more than the last index.
Read cart[1] = 'bread' and cart[len(cart) - 1] = cart[3] = 'eggs'.
len(cart) - 1 reaches the last item whatever the length is, which is safer than typing 3.
Apply cart[2] = 'juice': the box at index 2 now holds 'juice' instead of 'milk'.
Assigning to one index replaces just that value; the other three items and the length do not change.
Answer: len(cart) = 4, cart[1] = 'bread', and the last item is 'eggs'. After the change, cart = ['apple', 'bread', 'juice', 'eggs'].
Takeaway: An index is the number of steps from the start, so the last index is always the length minus 1.