Remember
A for loop takes successive values from an iterable. The body runs once for each value it receives.
A for loop takes successive values from an iterable. The body runs once for each value it receives.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = 'tea'
for item in items:
print(item)Top-level scope
'tea'Object 4 · str'a'A for loop takes successive values from an iterable. The body runs once for each value it receives.
(no output)
What do you expect this program to print?
items = "tea" NEXT ITEM --> "t" --> LOOP BODY NEXT ITEM --> "e" --> LOOP BODY NEXT ITEM --> "a" --> LOOP BODY NO ITEMS LEFT --> CONTINUE
Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = 'tea'
for item in items:
print(item)Top-level scope
'tea'Object 4 · str'a'The example has finished. Compare the final output with the values and references above.
t e a
What do you expect this program to print?
items = 'tea'
for item in items:
print(item)for variable in sequence:
# block of code
Here, variable takes the value of each item in sequence, one by one, and executes the indented block.
Each item in the list is printed on a new line. The loop automatically stops after the last element.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
apple
banana
cherry
range(5) generates numbers from 0 to 4. If you need to include 5, use range(6). Reference: range() bulitin function.
for i in range(5):
print("Step", i)
Step 0
Step 1
Step 2
Step 3
Step 4
Strings are iterable in Python—each character is treated as an element in the sequence.
for char in "Python":
print(char)
P
y
t
h
o
n
This version of range lets you control the start, stop, and increment values. It’s great for custom numeric patterns.
for i in range(2, 10, 2):
print(i)
2
4
6
8
The else clause runs after the loop completes normally. It’s skipped if the loop is exited via break.
for i in range(3):
print("Counting", i)
else:
print("Loop finished!")
Counting 0
Counting 1
Counting 2
Loop finished!
Nested loops let you work with multidimensional data or matrix-style problems.
for i in range(2):
for j in range(2):
print(f"i={i}, j={j}")
i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
The loop stops when num equals 5. Use break carefully—it ends the loop prematurely.
for num in range(10):
if num == 5:
break
print(num)
0
1
2
3
4
continue skips the current iteration and jumps to the next one.
for num in range(5):
if num == 2:
continue
print(num)
0
1
3
4
The enumerate() function is used to loop through the list names while keeping track of both the index and the value at the same time. In each iteration, index holds the position (0, 1, 2) and name holds the corresponding value from the list. This allows printing both index and name together.
names = ["Alice", "Bob", "Charlie"]
for index, name in enumerate(names):
print(index, name)
0 Alice
1 Bob
2 Charlie
range(len(list)) only if you need index values. Otherwise, loop directly over items.enumerate() if you need both index and item.Connect what you noticed in the example with the idea below.
A for loop takes successive values from an iterable. The body runs once for each value it receives.
A for loop must always count integers.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.