Lists & Strings as Sequences
Every variable so far has held exactly one value. A list holds many values in order, under one name — perfect for an inventory, a party roster, or anything else that grows over the course of a game. Strings turn out to behave a lot like lists too, once you know what to look for.
What You'll Learn
- Lists —
inventory = ["sword", "shield"], indexed from0, looped over withfor item in inventory: .append()andlen()— add an item, or count how many there are- Strings as sequences — indexing and slicing work the same way (
"dragon"[0:3]is"dra"), but strings are immutable — you can't change one in place
0, not 1 — on a 3-item list, list[3] is an IndexError, since the valid indexes are 0, 1, and 2.
Today's Activity
Today's practice happens inside the notebook below — work through it top to bottom:
- A worked example, fully written out
- Two guided practices — change a few values, then fill in some blanks
- Two independent practices — write your own from scratch, different scenario each time
- An optional stretch, if you want to go further
- One check-your-understanding question to close
Download the notebook, save it into your 1_foundations folder in your cloned repo, and open it in VS Code. Commit once you've saved it, then commit again when you're done for the day.
New to notebooks? See Using Jupyter Notebooks first.
What's Next
Coming up: 1.8 — Debugging Basics, then Project 1C — the final piece of your game.