✕ Close
← while loops

Level 5 · Lesson 38: Lists

Functions →
INTRODUCTION
Lists
One name, many values, in order.
press Enter ↵
🐍 main.py
Ctrl + Enter runs it too. Esc, then Tab, leaves the code.
Output

Press ▶ Run to see what your program does.

Lesson 38: Lists

Level 5, Python Thinks and Repeats. Many values under one name, counted from zero. The new word: list. It follows lesson 37, while loops. Next comes lesson 39, Functions.

What the lesson explains

Lists. One name, many values, in order.

pets[0] is the first. Square brackets make a list, and pick one item. On the card: pets = ["cat", "dog", "fish"]

What you do first

Add and look. Print the last pet, fish. The editor opens with: pets = ["cat", "dog", "fish"]. One way to write it: pets = ["cat", "dog", "fish"] / print(pets[2]). It prints: fish.

The code on the screen

pets[0] is the first. The editor shows 3 lines: pets = ["cat", "dog", "fish"] / print(pets[0]) / print(len(pets)). Run it, and it prints "cat", "3".

What you practise

Try it, "Add and look": Print the last pet, fish. The editor opens with: pets = ["cat", "dog", "fish"]. One way to write it: pets = ["cat", "dog", "fish"] / print(pets[2]). It prints: fish. Then: Add hamster, then print the list. The editor opens with: pets = ["cat", "dog", "fish"]. One way to write it: pets = ["cat", "dog", "fish"] / pets.append("hamster") / print(pets). It prints: ['cat', 'dog', 'fish', 'hamster'].

Try it, "Loop over a list": Print I love and each pet. The editor opens with: pets = ["cat", "dog", "fish"]. One way to write it: pets = ["cat", "dog", "fish"] / for pet in pets: / print("I love " + pet). It prints: I love cat / I love dog / I love fish.

Fix it, "IndexError": Print the third colour. The editor opens with: colours = ["red", "green", "blue"] / print(colours[3]). One way to write it: colours = ["red", "green", "blue"] / print(colours[2]). It prints: blue.

What PlayBuddy and PlayType say

PlayType: "A list is like a train. Each carriage carries one thing."

PlayBuddy: "Coders count from zero. The first carriage is number zero!"

Help in every lesson

Everything happens on a pretend computer in your browser, so nothing touches your real files. PlayBuddy and PlayType stand beside the board, cheer you on and say when it is your turn.

A green ring shows where to click the first time. After that you do it on your own, and if you slip twice the ring comes back.

Stars

The lesson has 4 things to do. Up to one slip in ten gives three stars, up to one in four gives two, and finishing always gives at least one. Stars and progress are saved in this browser only; there is no account.

About DayOneCode

DayOneCode is an independent learning site, not affiliated with or endorsed by Anthropic. Claude and Claude Code are trademarks of Anthropic.