Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the else block do in a for–else statement?
The else block runs only if the for loop completes all iterations without hitting a break statement.
Click to reveal answer
beginner
When is the else block skipped in a for–else loop?
The else block is skipped if the for loop is exited early using a break statement.
Click to reveal answer
beginner
True or False: The else block in a for–else loop runs after every iteration.
False. The else block runs only once after the loop finishes all iterations without a break.
Click to reveal answer
beginner
What is a real-life analogy for a for–else loop?
Imagine checking every item in a box for a defect. If you find a defect, you stop (break). If you check all items and find none defective, you celebrate (else).
Click to reveal answer
beginner
Write a simple Python for–else example that prints 'Found it!' if a number 5 is in a list, otherwise prints 'Not found!'.
numbers = [1, 2, 3, 4, 6]
for num in numbers:
if num == 5:
print('Found it!')
break
else:
print('Not found!')
Click to reveal answer
When does the else block run in a for–else loop?
AOnly if the loop contains a break
BAfter every iteration of the loop
CAfter the loop finishes all iterations without a break
DBefore the loop starts
✗ Incorrect
The else block runs only if the loop completes all iterations without encountering a break.
What happens if a break is executed inside a for loop with an else?
AAn error occurs
BThe <code>else</code> block is skipped
CThe loop restarts
DThe <code>else</code> block runs immediately
✗ Incorrect
If break is executed, the loop exits early and the else block does not run.
Which of these is a correct use of for–else?
ARunning code after every loop iteration
BSkipping loop iterations
CReplacing <code>if</code> statements
DSearching for an item and doing something if not found
✗ Incorrect
for–else is useful to run code if the loop did not find what it was searching for.
What will this code print?
for i in range(3):
if i == 1:
break
else:
print('Done')
ANothing
BDone
C0 1 2
DError
✗ Incorrect
The loop breaks when i == 1, so the else block does not run and nothing is printed.
Can for–else be used without a break statement?
AYes, but else always runs then
BNo, break is required
CYes, else never runs
DNo, syntax error
✗ Incorrect
If no break is used, the else block runs after the loop finishes.
Explain how the else block works in a for–else loop and when it runs.
Think about what happens if the loop never breaks.
You got /3 concepts.
Describe a real-life situation where a for–else loop would be useful.
Imagine checking items one by one for a problem.
You got /3 concepts.
Practice
(1/5)
1.
What happens to the else block in a for-else statement if the loop completes all iterations without a break?
easy
A. The else block runs after the loop finishes.
B. The else block never runs.
C. The else block runs before the loop starts.
D. The else block runs only if the loop has a break.
Solution
Step 1: Understand the for-else structure
The else block in a for loop runs only if the loop finishes all iterations without encountering a break.
Step 2: Apply to the question
If the loop completes normally, the else block executes after the loop ends.
Final Answer:
The else block runs after the loop finishes. -> Option A
Quick Check:
For-else else runs if no break [OK]
Hint: Else runs only if loop ends without break [OK]
Common Mistakes:
Thinking else runs always
Believing else runs before loop
Confusing else with else-if
2.
Which of the following is the correct syntax for a for-else statement in Python?
___
print(i)
else:
print("Done")
easy
A. foreach i in range(3):
B. for i range(3):
C. for i in range(3):
D. for i to range(3):
Solution
Step 1: Recall correct for loop syntax
Python uses for variable in iterable: syntax. So for i in range(3): is correct.
Step 2: Check options
Only for i in range(3): matches correct syntax; others have syntax errors.
Final Answer:
for i in range(3): -> Option C
Quick Check:
Correct for loop syntax uses 'in' [OK]
Hint: Remember: for variable in iterable: [OK]
Common Mistakes:
Omitting 'in' keyword
Using 'to' instead of 'in'
Using 'foreach' which is not Python
3.
What is the output of this code?
for num in [1, 2, 3]:
if num == 4:
break
else:
print("No break encountered")
medium
A. No output
B. No break encountered
C. break
D. Error
Solution
Step 1: Analyze the loop and condition
The loop checks if num == 4. Since 4 is not in the list, break never runs.
Step 2: Check the else block execution
Because the loop completes without break, the else block runs and prints "No break encountered".
Final Answer:
No break encountered -> Option B
Quick Check:
Else runs if no break, so prints message [OK]
Hint: Else runs only if break never happens [OK]
Common Mistakes:
Assuming else never runs
Thinking break triggers else
Expecting error due to missing break
4.
Find the error in this code snippet:
for i in range(3):
if i == 1
break
else:
print("Loop ended")
medium
A. Missing colon after if condition
B. Missing colon after for loop
C. Indentation error in else block
D. break cannot be used inside for loop
Solution
Step 1: Check syntax of if statement
The if statement is missing a colon at the end of the condition line.
Step 2: Verify other parts
The for loop and else block have correct colons and indentation; break is valid inside loops.
Final Answer:
Missing colon after if condition -> Option A
Quick Check:
if statements require colon [:] [OK]
Hint: Check colons after if, for, else lines [OK]
Common Mistakes:
Ignoring missing colon
Thinking break is invalid
Confusing indentation errors
5.
Consider this code:
def find_even(numbers):
for n in numbers:
if n % 2 == 0:
print(f"Found even: {n}")
break
else:
print("No even number found")
find_even([1, 3, 5])
What will be printed when calling find_even([1, 3, 5])?
hard
A. Found even: 3
B. Found even: 1
C. No output
D. No even number found
Solution
Step 1: Trace the loop with given list
The list has only odd numbers: 1, 3, 5. None satisfy n % 2 == 0, so break never runs.
Step 2: Check else block execution
Since no break occurred, the else block runs and prints "No even number found".
Final Answer:
No even number found -> Option D
Quick Check:
Else runs if no break, so prints no even found [OK]