Recall & Review
beginner
What is a for loop used for in Python?
A for loop is used to repeat a block of code multiple times, once for each item in a sequence like a list or string.
Click to reveal answer
beginner
How does Python decide how many times to run the code inside a for loop?
Python runs the code once for each item in the sequence you give it. It stops when it reaches the end of the sequence.
Click to reveal answer
beginner
What happens to the loop variable in each iteration of a for loop?
The loop variable takes the value of the next item in the sequence each time the loop runs.
Click to reveal answer
beginner
Explain the flow of a for loop with an example:
for x in [1, 2, 3]: print(x)
The loop runs 3 times. First, x = 1 and prints 1. Then x = 2 and prints 2. Finally, x = 3 and prints 3. Then it stops.
Click to reveal answer
beginner
Can a for loop run zero times? When?
Yes, if the sequence is empty (like an empty list), the for loop does not run at all.
Click to reveal answer
What does the loop variable represent in a for loop?
✗ Incorrect
The loop variable takes each item from the sequence one by one during the loop.
How many times will this loop run?
for i in []: print(i)
✗ Incorrect
The sequence is empty, so the loop does not run at all.
What happens after the for loop finishes all items?
✗ Incorrect
After the loop finishes, the program moves on to the next instruction after the loop.
Which of these is a valid sequence to use in a for loop?
✗ Incorrect
For loops need a sequence like a list, string, or tuple to loop over.
What is printed by this code?
for c in 'Hi': print(c)
✗ Incorrect
The loop prints each character of the string 'Hi' on its own line.
Describe how a for loop works in Python, including what the loop variable does.
Think about how the loop variable changes each time.
You got /4 concepts.
Explain what happens if the sequence in a for loop is empty.
What does the loop do when there is nothing to loop over?
You got /3 concepts.