Python for Humans · Draft v0.1 · Oct 27, 2026 · Draft position: chapter 4 · First draft; introduce the Python panel through a small adventure inventory before reading files.
Keeping Things Together
- You should already know
- Variables, input and strings (chapter 1); decisions (chapter 2); while loops (chapter 3).
- 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 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 9.
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 3, 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. Clear screen clears the display and keeps those values; Restart starts Python afresh and forgets them. 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. The panel gives us a convenient place to try short ideas and inspect their results.
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
len() is short for length. For a list, it counts the items.
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.
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.
Add Something
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
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.
Your turn: Add another companion to your own list. Does the last position now hold the same name as before? Check with companions[-1].
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:
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.")
Remove 'torch', from the list and run it again. Does the other branch run? Then change the program to check for a different supply.
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.
supplies = ['torch', 'map', 'pizza', 'rope'] for item in supplies: print(f"Packed: {item}") print("Ready for the castle!")
Run it. 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.
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.
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.
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.
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?
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
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: 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?
Your Turn
Predict
Write down both lines of output before running this:
supplies = ['torch', 'map'] supplies.append('pizza') print(len(supplies)) print(supplies[-1])
Find the Bug
This should print the first supply. It runs, but chooses the wrong one. Fix the position.
supplies = ['torch', 'map', 'pizza'] print(supplies[1])
Make a Packing Check
In the box below, write a list of at least three supplies. Then add a loop to display each one, and a decision that prints a special message when you have an item the dragon would like.
supplies = ['torch', 'map', 'pizza']
You can borrow the loop from Check Every Supply and the decision from Is It in the Bag. First get the loop working; then add your check. Test once with the special item present and once without it.
If you'd like another challenge, ask the player for one more supply with input() and append their answer before displaying the list.
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.