Unit 3: Loops & Lists
Do things again and again.
Unit 3 of 31 in Python for kids. Its 6 lessons are Repeating, Keep Going Until, Lists, Functions, Sending an Answer Back and Guessing Game — below is everything each one explains, and a question or two from it to try.
Every sample on this page was run through real Python before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🔁 Repeating
Lazy is good
Never write the same line five times. A for loop repeats it for you. range(3) counts 0, 1, 2 — it starts at zero and stops *before* the number.
Python
for i in range(3):
print(i)
It prints
0 1 2
Try it yourself
How many lines does this print?
Python
for i in range(4):
print("hi")
- 3
- 4
- 5
- 1
What is the LAST number this prints?
Python
for i in range(5):
print(i)
Answer them in the app
♾️ Keep Going Until
When you do not know how many times
A for loop needs to know the count up front. A while loop just keeps going as long as something stays True — perfect when you cannot know in advance.
Python
lives = 3
while lives > 0:
print(lives)
lives = lives - 1
It prints
3 2 1
Jumping out early
break leaves the loop immediately, whatever the test says. Handy when you find what you were looking for.
Python
for n in [3, 7, 2, 9]:
if n > 5:
print(f"found {n}")
break
It prints
found 7
Try it yourself
What is the LAST number this prints?
Python
n = 1
while n < 20:
print(n)
n = n * 2
What goes wrong here?
Python
n = 5
while n > 0:
print(n)
- Nothing, it prints 5 four times
- n never changes, so it prints forever
- while needs a colon
- It prints nothing
Answer them in the app
📋 Lists
A box that holds many things
A list holds lots of values in order, inside square brackets. Counting starts at 0, so the first thing is number 0.
Python
pets = ["cat", "dog", "fish"]
print(pets[0])
print(len(pets))
It prints
cat 3
Looping over a list
A for loop can walk through a list directly. Each turn, the variable holds the next item.
Python
for pet in ["cat", "dog"]:
print(pet)
It prints
cat dog
Try it yourself
What does colors[1] give?
Python
colors = ["red", "green", "blue"]
- red
- green
- blue
- 1
What does this print?
Python
nums = [4, 8, 15]
print(len(nums))
Answer them in the app
🧰 Functions
Make your own commands
A function is code you name once and use whenever you like. def makes it. Nothing happens until you *call* it by writing its name with brackets.
Python
def cheer():
print("Woohoo!")
cheer()
cheer()
It prints
Woohoo! Woohoo!
Passing things in
Words inside the brackets are parameters — stuff you hand to the function each time you call it.
Python
def greet(name):
print("Hi " + name)
greet("Zoe")
It prints
Hi Zoe
Try it yourself
What does this print?
Python
def hello():
print("hi")
- hi
- hello
- nothing — it was never called
- an error
Answer it in the app
↩️ Sending an Answer Back
Printing is not answering
So far your functions have *printed*. A function that returns hands the answer back, so you can store it, add to it, or pass it on. That is what makes functions properly useful.
Python
def double(n):
return n * 2
answer = double(5)
print(answer + 1)
It prints
11
A function with no return
Leave return out and Python quietly hands back None, which means "nothing here". That is the usual cause of a mysterious None appearing in your output.
Python
def shout(word):
print(word + "!")
result = shout("hi")
print(result)
It prints
hi! None
Try it yourself
What is the difference between print and return?
- They are the same
- print shows it on screen; return hands it back so the rest of the code can use it
- return is faster
- print only works with numbers
What does this print?
Python
def add(a, b):
return a + b
print(add(2, 3) + add(1, 1))
Answer them in the app
🏆 Guessing Game
Asking the player something
input() waits for the player to type a line. It always gives you text, so wrap it in int() when you need a number.
Python
guess = int(input())
print(guess + 1)
It prints
If the player types 4, this prints 5
Try it yourself
What is the last thing this prints?
Python
for i in range(3):
print(i * 2)
Answer it in the app