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 from 0, looped over with for item in inventory:
  • .append() and len() — 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
Watch out for: indexing starts at 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:

  1. A worked example, fully written out
  2. Two guided practices — change a few values, then fill in some blanks
  3. Two independent practices — write your own from scratch, different scenario each time
  4. An optional stretch, if you want to go further
  5. 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.

Download 1.7_Lists_and_Strings.ipynb

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.