Lesson 3 of 145 min
Lists, loops and if
A list keeps values in order; a for loop runs the indented lines under it once for each value, and an if runs its own only when its test is True.
After this lesson you can read and write a list, a for loop and an if, and say which lines run.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
else: when the test is False
An if can say what to do when its test is False too. The lines under else run only then. else lines up with its if, and its lines are indented under it, four spaces, like every block. Your editor puts the four spaces in for you.
scores = [9, 2, 7]
for score in scores:
if score > 5:
print(score, "spam")
else:
print(score, "ok")9 spam 2 ok 7 spam
Same value, and two tests at once
One = keeps a value under a name. Two, ==, ask whether two values are the same, and give True or False, as > does. and is True only when both tests are True. or is True when at least one is.
score = 7
print(score == 7)
print(score > 5 and score > 8)
print(score > 8 or score == 7)True False True
How many items, and every position
len() counts the items of a list. range() gives the positions from zero up to that number, without the number itself. Real code loops over the positions this way when it needs both the position and the item.
scores = [9, 2, 7]
print(len(scores))
for position in range(len(scores)):
print(position, scores[position])3 0 9 1 2 2 7
The last item: position -1
Python also counts from the end. Position -1 is the last item and -2 the one before it, however long the list is. You will see [-1] often in AI code, to take the newest result or the last word.
scores = [9, 2, 7]
print(scores[-1])
print(scores[-2])7 2
Run on Python 3.13.3. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
The spam filter: scores = [9, 2, 7], then for score in scores:, and under it if score > 5: with one print line. How many lines does it print?
2 of 2
What does this print?
scores = [9, 2, 7]
print(scores[1])The ideas in this lesson
- Lists
- A list keeps values in order, in square brackets. Each has a position, counted from 0.
- For loops
- A for loop runs the indented lines under it once for each item of a list, in order.
- if
- A comparison gives True or False, and `if` runs the indented lines under it only when it is True.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
The puzzle
Here's a puzzle. A spam filter gives every email a score. The higher the score, the more it looks like spam. These five lines check the scores of three emails. And there's only one print line. So how many lines will it print?
A list
Keep that number in mind. First, the scores: nine, two and seven. How does the code hold all three?
Line one puts them in square brackets, in order. They all go under one name, like any variable: scores.
Many values, in order, under one name... is called a list.
Position zero
So how do you get one score out? Ask for its position, in square brackets. But Python counts from zero. So position zero is the first score, nine.
Line two keeps it as pick.
Your turn
Your turn. What's at position one?
Two, the second score. If you said nine, you counted from one. And the third score, seven, is at position two.
A list, by position... from zero.
A for loop
Now, how do you check every score, not just one?
Line three says: for score in scores. The two lines under it start further to the right. They're indented. And they run once for each score.
The first of them is a test, with a number in it: the limit. We'll come back to it. For now, the limit is zero, so every score gets through.
Each time round, the loop gives the next score a name: score. The first time, score is nine.
Three passes
Watch it run.
Nine... spam. Back to line three. Two... spam. Then seven... spam.
Three times round. Three lines.
If you thought a print line runs only once: a loop runs its lines again, for each item.
True or False
So how does the filter tell spam from the rest? The limit goes back to five. The test asks: is the score more than five? That sign means "more than". For nine... True. For two... False.
True and False are Python's yes and no.
if
If runs the indented lines under it... only when its test is True. True: line five runs. False: Python skips it.
So now, how many lines?
Nine: True. Spam.
Two: False. Line five is skipped.
Seven: True. Spam.
Two lines. The test ran three times... and line five ran twice. If you said three, the if let only two scores through to line five.
The limit decides
Raise the limit to eight. Now only nine is more than eight. One line.
The limit decides.
The spaces
Do the spaces really matter? Here, line five is not indented under the if. Will it still run?
It stops, before any line runs. Indentation error. Python expected an indented block. A block is the lines that belong to the if... and this if has none.
The fix: indent it, further than the if.
A real filter
Do real filters work like this? One does, on many mail servers. It adds up a score for each email... and its limit is five.
A score, a limit, and an if.
Every item, and only when True
So here's what you saw. A list keeps values in order, from position zero. A for loop runs its indented lines for every item. And if runs its lines only when its test is True.
The lab
That's the end of this unit. In its lab, you use these three lessons to score a chatbot's answers.
A loop and an if
So a loop looks at every item. An if picks the ones it wants. Now try it on this lesson's page, and take the quiz. Make line two stop with an error. Look at the positions.
You use this lesson in Lab 1: Score a chatbot's answers, which comes next.
Facts last checked on .
