Control Flow: Loops Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Control Flow: Loops flashcards as text
What does the `else` clause of a `for` loop execute?
Answer: Only if the loop completed without a `break`
The `else` clause runs after the loop finishes normally (without hitting a `break` statement).
What is the output of: `for i in range(3): print(i, end=' ')`?
Answer: 0 1 2
`range(3)` generates 0, 1, 2 — it stops before 3.
Which loop construct is most appropriate when the number of iterations is unknown ahead of time?
Answer: `while` loop
A `while` loop continues as long as a condition is true, making it ideal when iteration count is unknown.
What happens if the condition of a `while` loop is never `False`?
Answer: The loop runs indefinitely (infinite loop)
If the condition never becomes `False` and there's no `break`, the `while` loop runs forever.
What does `continue` do inside a loop?
Answer: Skips the rest of the current iteration and moves to the next
`continue` skips the remaining statements in the current loop body and proceeds to the next iteration.
Given `x = 0`, what does `while x < 5: x += 2` leave `x` as after the loop?
Answer: 6
x goes 0→2→4→6; when x=6, the condition 6<5 is False, so the loop ends with x=6.
What is the output of: `for i in range(10, 0, -3): print(i, end=' ')`?
Answer: 10 7 4 1
`range(10, 0, -3)` yields 10, 7, 4, 1 — stepping by -3 and stopping before reaching 0.