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 `zip([1,2,3], ['a','b'])` produce when iterated?
Answer: (1,'a'), (2,'b')
`zip()` stops at the shortest iterable, so only (1,'a') and (2,'b') are produced.
What is a list comprehension equivalent to `for x in range(5): result.append(x**2)`?
Answer: `[x**2 for x in range(5)]`
A list comprehension with square brackets `[expr for var in iterable]` produces a list.
Which of the following correctly uses `while` with an `else` clause?
Answer: `while x > 0: x -= 1 else: print('done')`
The `else` clause is at the same indentation level as `while`, not inside the loop body.
How many times does this loop print? ``` for _ in range(3): for _ in range(2): print('x') ```
Answer: 6
The inner loop prints 2 times per outer iteration, and the outer runs 3 times: 3×2 = 6.
What does `range(5, 5)` produce?
Answer: An empty range
When start equals stop, `range()` produces no values — it's an empty sequence.
What is the output of: ``` x = [1, 2, 3] for i in x: x.append(i) if len(x) > 5: break print(len(x)) ```
Answer: 6
The loop appends until len(x) > 5, so it stops when len is 6 (after adding two elements).
Which built-in function returns an iterator that applies a function to every item of an iterable?
Answer: `map()`
`map(func, iterable)` applies `func` to each element and returns an iterator of results.