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 `enumerate(['a', 'b', 'c'])` yield when iterated?
Answer: (0, 'a'), (1, 'b'), (2, 'c')
`enumerate()` yields `(index, value)` tuples starting from index 0 by default.
Which statement about nested loops is true?
Answer: `break` in the inner loop only exits the inner loop
`break` only exits the innermost loop containing it; the outer loop continues normally.
What is the result of `sum(i for i in range(5))`?
Answer: 10
`range(5)` is 0+1+2+3+4 = 10.
How do you iterate over both keys and values of a dictionary `d` simultaneously?
Answer: `for k, v in d.items():`
`d.items()` returns key-value pairs as tuples, which can be unpacked in the loop header.
What is the output of: `i = 0 while True: i += 1 if i == 3: break print(i)`?
Answer: 3
The loop breaks when `i` becomes 3, so `print(i)` outputs 3.
What does `pass` do when used inside a loop body?
Answer: Does nothing — acts as a placeholder
`pass` is a no-op statement used as a syntactic placeholder when a statement is required but no action is needed.
What is the output of: ``` for i in range(4): if i == 2: continue print(i, end=' ') ```
Answer: 0 1 3
`continue` skips the `print` when `i == 2`, so 2 is not printed.