Python for Humans · Draft v0.4 · Oct 28, 2026 · Draft position: chapter 5 · Add an independent lunch-order checkpoint with optional hints and a worked explanation.
Keeping Things Together
Lists & Indexing
- You should already know
- Variables, input and strings (chapter 1); decisions (chapter 3); while loops (chapter 4).
- You'll build
- A collection of supplies for your castle adventure, and a program that checks what you've packed.
- You'll be able to
- Create a list, access and replace items by position, add items, check membership, and use a for loop to process a collection.
- You need
- This page. Python runs in the panel beside it; installing Python on your own computer comes in chapter 17.
Our adventure can remember which door you chose. But what if you want to bring a torch, a map, and something to eat?
We could give each supply a separate variable. But then we'd need more names every time we packed something new. What if we wanted to display everything we were carrying?
A list keeps several items together under one name. Let's make one and explore it.
Meet the Python Panel
Until now, you've run whole programs in the boxes on the page. In chapter 4, you also wrote more of those programs yourself.
For our next task, we'll use the Python panel beside the page. On a small screen, click the Python button to open it. We'll create a small collection, inspect it, and change it one command at a time.
Wait until the panel says Python is ready. Click beside the >>> prompt, type 2 + 2, and press Enter.
>>> 2 + 2 4
Type only the part after >>>. The prompt and the answer are shown here so you can compare them with your panel. You don't need a Run button: pressing Enter runs the command.
Notice that Python displays 4 without print(). In this panel, when you enter an expression, Python shows its value.
Now type these two commands yourself, pressing Enter after each one:
>>> message = 'Welcome to the castle!' >>> message 'Welcome to the castle!'
The assignment doesn't display anything. But the next command shows the value of message: Python remembers it between commands. The quotation marks show that the result is a string.
Try asking for message again. Then give it a different string and ask again. You can use the Up arrow to bring back a previous command, edit it, and press Enter.
If you make a mistake, read the error and try again at the next prompt. An ordinary error doesn't erase the values you've already created. Restart clears the display, starts Python afresh, and forgets those values. If you restart or reload the page, type the commands that create your values again.
You can still use the editable program boxes for longer programs. Each Run starts its box with fresh variables, separate from the panel and other boxes. The browser saves the code you edit in those boxes, but it doesn't save the variables or output from a run. To keep a separate copy, select your code and copy it into a plain-text file.
The panel gives us a convenient place to try short ideas and inspect their results. Its commands and variables aren't saved when you leave or reload the page.
Pack Your Supplies
Type this into the panel, then ask for supplies:
>>> supplies = ['torch', 'map', 'pizza'] >>> supplies ['torch', 'map', 'pizza']
The square brackets mark the beginning and end of the list. Commas separate its items. Here each item is a string, so each has its own quotation marks.
We've given the whole list one name, supplies. Python keeps the items in the order we wrote them.
Ask how many items it contains:
>>> len(supplies) 3 >>> # Your turn: Create another list called companions, containing the >>> # names of two people or creatures you'd take to the castle. Type a >>> # command to display it, then another to count its items.
len() is short for length. For a list, it counts the items.
Pick One Item
How do we ask for just the torch?
>>> supplies[0] 'torch' >>> supplies[1] 'map' >>> supplies[2] 'pizza'
The number in square brackets is the item's index, its position in the list. Python starts at 0, so the first item is at position 0, the second at 1, and the third at 2.
You can also count backward. -1 means the last item:
>>> supplies[-1] 'pizza' >>> # Try it: Ask for supplies[3]. We have three items, but no item at >>> # index 3, so Python reports an IndexError. The list is still there. >>> # Ask for supplies[0] again to check.
Now inspect each item in your companions list. Predict which name you'll get before pressing Enter.
Replace One Item
Suppose we'd rather pack a lantern than a torch. We can assign a new value at one position:
>>> supplies[0] = 'lantern' >>> supplies ['lantern', 'map', 'pizza'] >>> # Try it: Change the first supply back to 'torch', then ask for >>> # supplies[0] to check. We'll use exactly this kind of change when a >>> # player puts an X on a tic-tac-toe board.
Reading supplies[0] asks what's there. Writing supplies[0] = 'lantern' changes that entry in the existing list. The other positions stay the same, and the list still has three items.
Add Something
Start with the original supplies again, so we have the same collection:
>>> supplies = ['torch', 'map', 'pizza']
We've forgotten a rope. We could rewrite the list, but Python gives lists an ability called append():
>>> supplies.append('rope') >>> supplies ['torch', 'map', 'pizza', 'rope'] >>> len(supplies) 4 >>> # Your turn: Add another companion to your own list. Does the last >>> # position now hold the same name as before? Check with >>> # companions[-1].
append() adds one item at the end. The dot means we're using an ability of supplies. Programmers call an ability written this way a method.
Notice that supplies.append('rope') didn't display a result. It changed the list. Asking for supplies afterward let us see that change.
Keep the shared supplies list as it is for now so your results match the examples below.
Is It in the Bag?
Suppose we need a torch to enter the castle. The word in lets us check whether a list contains an item:
>>> 'torch' in supplies True >>> 'umbrella' in supplies False
These are the same True and False values we met with comparisons. We can use the answer in a decision:
# Remove 'torch', from the list and run it again. Does the other # branch run? Then change the program to check for a different # supply. # Each Run starts fresh. supplies = ['torch', 'map', 'pizza', 'rope'] if 'torch' in supplies: print("You light your torch and enter the castle.") else: print("It's too dark. Better find a torch first.")
The panel is handy for small questions. This program box lets us edit several connected instructions and run them together, just as in the earlier chapters.
Check Every Supply
What if we want to display each item? We could write print(supplies[0]), then repeat it for each position. But that would mean changing the program whenever the list grew.
A for loop can visit every item for us. If you tried the optional five-guess game, you'll recognize the shape. This time we're repeating over a list instead of a range of numbers.
# Run it. # Your turn: Change the list in the box to your companions' names, # rename item to companion in both places, and make the message # welcome each one. Add a name and check that it gets a greeting # too. # Each Run starts fresh. supplies = ['torch', 'map', 'pizza', 'rope'] for item in supplies: print(f"Packed: {item}") print("Ready for the castle!")
On the first trip through the loop, item refers to 'torch'. On the second, it refers to 'map', then 'pizza', then 'rope'. When there are no items left, the loop ends and Python prints the final message.
You choose the name item; it isn't a special Python word. It names the current item, while supplies names the whole collection.
When you want to repeat until something happens, a while loop is useful. When you want to do something with every item in a collection, a for loop is useful.
Build a New List
Let's pack for a dragon who is interested only in food. We can combine a loop and a decision to select matching items.
# Try it: Add another 'pizza' to the supplies in this box. Predict # the new food list. Then change the decision to collect torches # instead. What happens when nothing matches? # Each Run starts fresh. supplies = ['torch', 'pizza', 'map', 'pizza', 'rope'] food = [] for item in supplies: if item == 'pizza': food.append(item) print(food)
[] creates an empty list. Each time the loop finds 'pizza', it adds that item to food. We end with ['pizza', 'pizza'].
A list can contain the same value more than once. Those are two separate entries, and our loop visits both.
Count Matching Items
Back in the panel, let's try a list with repeated items:
>>> snacks = ['pizza', 'apple', 'pizza'] >>> snacks.count('pizza') 2 >>> snacks.count('pie') 0 >>> # Your turn: Add 'Pizza' to snacks with append(). Before checking, >>> # predict what snacks.count('pizza') will return. Does changing the >>> # capitalization in your search change the answer?
count() is another list method. It counts items equal to the value you give it. It doesn't count the letters 'pie' inside a longer string, and it treats 'Pizza' and 'pizza' differently.
Your Turn
Predict
# Each Run starts fresh. # Write down both lines of output before running this: # Write your prediction below before running. # After the first run, edit the code and predict again. supplies = ['torch', 'map'] supplies.append('pizza') print(len(supplies)) print(supplies[-1])
Find the Bug
# This should print the first supply. # Fix the position used below. # Each Run starts fresh. supplies = ['torch', 'map', 'pizza'] print(supplies[1])
On Your Own: Collect Lunch Orders
Start without copying an earlier program. This also brings back something from Loops: you don't know how many answers you'll receive.
# You're collecting lunch orders for a few friends. Write a program # that keeps asking for an order until someone types done. Then # display every order and the total number of orders. Two people can # order the same thing; keep both orders. Don't include done in the # collection. # Check two separate runs: # - Enter soup, salad, soup, then done. You should see those three # orders, in that order, and a total of 3. The questions should stop # after done. # - Enter done immediately. You should see a total of 0 and no # orders. # Each Run starts fresh. # Write your lunch-order program here.
Explain why your program keeps the earlier answers when it asks the next question. If you're stuck, open one hint at a time.
Hint: What needs to survive between questions?
Start with an empty list before the repeating questions. Creating that list again inside the loop would discard the orders you've already collected.
Hint: How do the questions end?
A while True: loop can ask a question on each iteration. Check the answer for done and use break before adding it to the list. After the loop, a for loop can display the orders, and len() can count them.
Compare a worked approach after you've tried
# Each Run starts fresh. orders = [] while True: order = input('What would you like? (done to finish) ') if order == 'done': break orders.append(order) for order in orders: print(order) print('Total orders:', len(orders))
The list is created once. Each new answer replaces the value of order, but append() adds an entry to the same list. We check for done before appending, so it never becomes an order. Both display instructions come after the while loop; they run when the questions are finished.
Your names and messages can differ. Compare what repeats and what happens only once, then try your own version again with different orders.
What I've Learned
Write one sentence explaining the difference between a list and the current item in a loop over that list. Include a tiny example using your own names.
What's Next?
A list can hold a few adventure supplies, but it can also hold hundreds of thousands of words. We wouldn't want to type them all between square brackets.
In Reading a File, we'll load Shakespeare into Python, split the text into a list, and use the skills you've just practiced on a much bigger collection.
Projects to Build Next
- Tic-Tac-Toe: Your Move — Make a board remember moves, then let two players take turns.