โ† All Python Flashcard Decks

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
  1. 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.

  2. 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.

  3. 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()`.

  4. 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.

  5. 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.

  6. 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.

  7. 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.