Bird
0
0

What does the else block do in a for-else statement in Python?

easy📝 Conceptual Q1 of 15
Python - For Loop
What does the else block do in a for-else statement in Python?
AExecutes after every iteration of the loop
BExecutes before the loop starts
CExecutes only if the loop contains a <code>break</code>
DExecutes only if the loop completes without a <code>break</code>
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Analyze when else executes

    If the loop is broken early by break, the else block is skipped. Otherwise, it runs after the loop ends.
  3. Final Answer:

    Executes only if the loop completes without a break -> Option D
  4. Quick Check:

    for-else else runs if no break [OK]
Quick Trick: Else runs only if loop ends normally without break [OK]
Common Mistakes:
MISTAKES
  • Thinking else runs after every loop iteration
  • Believing else runs only if break occurs
  • Assuming else runs before the loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes