Sequence Patterns and Mutability
Scenario: Changing a nested list unexpectedly changes another variable that points to it.
New words, in plain English
- Reference - a connection from a name to an object
- Shallow copy - a new outer collection that still shares nested objects
- Sequence - an ordered value supporting indexes
Assignment does not copy a list; both names point to one object. enumerate supplies index-value pairs, zip walks collections together, and membership checks use in.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
names = ["api", "db"]
for number, name in enumerate(names, start=1):
print(number, name)
ports = dict(zip(names, [80, 5432]))
Tip: When nested independence matters, learn copy.deepcopy, but prefer simple immutable shapes when possible.
Goal: Practice this idea in the py-list-basics challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.