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 is the output of: ``` for i in range(1, 6): if i % 2 == 0: continue print(i, end=' ') ```
Answer: 1 3 5
`continue` skips even numbers, so only odd numbers 1, 3, 5 are printed.
What is the `_` variable commonly used for in a `for` loop like `for _ in range(5)`?
Answer: It indicates the loop variable is intentionally unused
By convention, `_` is used when the loop variable value is not needed in the loop body.
What does `iter()` return when called on a list?
Answer: A list iterator object
`iter(list)` returns a list iterator that produces elements one at a time via `next()`.
What is the output of: ``` result = [x for x in range(10) if x % 3 == 0] print(result) ```
Answer: [0, 3, 6, 9]
Numbers from 0-9 divisible by 3 are 0, 3, 6, 9.
In a `for` loop, what happens to the loop variable after the loop completes?
Answer: It retains the last value it was assigned
Python does not delete the loop variable after a `for` loop; it holds the last value from the iteration.
Which is the correct way to loop through a string character by character?
Answer: Both B and C
Both `for char in 'hello':` and `for char in list('hello'):` iterate character by character.
What does `while not done:` do if `done = False`?
Answer: Executes the loop body (since `not False` is `True`)
`not False` evaluates to `True`, so the `while` condition is satisfied and the loop body runs.