for number in [1, 2, 3]:
if number == 2:
break
print(number)Line-by-line explanation
- The loop begins with 1.
- When number becomes 2, the condition is True.
breakends the loop.- Only 1 is displayed.
for number in [1, 2, 3]:
if number == 2:
continue
print(number)Line-by-line explanation
- The loop visits each number.
- For 2, continue skips the print line.
- The output contains 1 and 3.
Tiny Practice
Skip the word “bad” and display the others.
Starter template
for word in ["good", "bad", "great"]:
if word == "bad":
CHANGE_ME
print(word)Line-by-line explanation
- The if identifies bad.
- The missing instruction should skip only that iteration.
Hint
- Use
continue. - Keep it indented under if.
- Do not use break.
Show Solution
for word in ["good", "bad", "great"]:
if word == "bad":
continue
print(word)Line-by-line explanation
- Good is displayed.
- Bad triggers continue and is skipped.
- Great is displayed.