Control Flow Constructs
Branching and looping at the statement level: structural pattern matching, for and while with their else clause, and the expression forms a decision can take. Interviewers read fluency from these.
part ofPythonoverview, primer and where to startread it →on this pageshowhide
explore
- Iteration Statements27 questions
- enumerate and zip Idioms4 questions
- The Loop else Clause3 questions
- Mutating a Live Collection4 questions
- while Loops and Termination4 questions
- range and Index Loops4 questions
- Early Exit From Loops4 questions
- Recursion Versus Looping4 questions
- Decision Structures16 questions
- Conditional Expressions4 questions
- Assignment Expressions4 questions
- Dict Dispatch Tables4 questions
- Flattening if/elif Chains4 questions
- Destructuring Syntax22 questions
- The match Statement4 questions
- Capture Versus Value Patterns3 questions
- Sequence and Mapping Patterns4 questions
- Class Patterns and __match_args__4 questions
- Guards and Or-Patterns4 questions
- Wildcards and Exhaustiveness3 questions
questions
65 · 3 sectionsWhat is the difference between `break` and `continue` in a Python loop?
basics
~20 sbreak ends the innermost enclosing loop immediately and control resumes after it. continue abandons only the rest of the current pass and moves on to the next item or condition test. Neither statement touches an outer loop.
What does Python's enumerate() yield, and what does its start argument change?
basics
~10 senumerate() wraps any iterable and lazily yields (count, item) two-tuples. The start argument sets the first count, so start=1 numbers items from one. It shifts only the counter, never which items come out.
Why does calling list.remove inside a for loop over that same list skip elements?
basics
~20 sA list iterator holds an integer index and advances it on every pass. Deleting an element shifts everything after it down one slot, so the next index lands one element too far and the shifted item is never visited.
When does the else block of a Python for or while loop actually run?
basics
~20 sThe else clause of a for or while loop runs when the loop ends normally: the iterable is exhausted or the while condition turns false. A break skips it, and an empty iterable still triggers it.
What does Python's range(10, 0, -1) produce, and when is a range empty?
basics
~20 srange(10, 0, -1) yields 10 down to 1: start is included and stop excluded, so 0 never appears. A range is empty whenever the step cannot carry start toward stop, as in range(0, 5, -1).
In Python's `a if cond else b`, what is evaluated and in what order?
basics
~20 sPython evaluates cond first. If it is truthy, only a is evaluated and becomes the value; otherwise only b is. The branch that is not chosen never runs, so its side effects and its exceptions never happen.
Why does a Python dict dispatch table store `handler` rather than `handler()` as its values?
basics
~20 sA dict literal evaluates every value eagerly while the dict is being built, so handler() would run the handler right there and store its return value. Storing the bare name keeps the function object, which you call after the lookup.
How does Python decide which branch of an if/elif/else chain executes?
basics
~10 sConditions are tested top to bottom and the first truthy one wins: its block runs and the whole rest of the chain is skipped. If none is true, the optional else block runs.
What does Python's walrus operator `:=` do, and how does it differ from `=`?
basics
~20 s:= is an assignment expression: it binds a name and also evaluates to the assigned value, so it can be used inside a condition or a call. Plain = is a statement, which produces no value at all.
How does Python parse `'Result: ' + status if ok else 'fail'`, and why is that a bug?
basics
~20 sThe conditional expression binds looser than +, so Python reads it as ('Result: ' + status) if ok else 'fail'. When ok is falsy the prefix silently disappears. Parenthesize the conditional expression so it is a single operand of the concatenation.
Why does `case RED:` in a Python match statement rebind RED instead of comparing against it?
basics
~20 sA bare name in a case is a capture pattern: it always matches and binds the subject to that name, comparing nothing. To compare against a named constant you must write a dotted value pattern such as Color.RED.
What does `case _:` match in a Python match statement, and where must it appear?
basics
~20 scase _: is the wildcard pattern: it matches any subject and binds nothing, so the name _ keeps whatever value it already had. Because it always succeeds, it must be the last case in the match statement.
What does the `if` guard on a `match` statement's `case` clause do?
basics
~20 sA guard is an extra if expression written after a case pattern. It is checked only once that pattern has matched and bound its names; a falsy guard rejects the case and matching moves on to the next one.
In a Python match statement, what does `case [a, b, *rest]` match, and what does it bind?
basics
~20 sIt matches any sequence of at least two items - a list, tuple, range, deque and so on, but never a str or bytes. The first two items bind to a and b, and everything left over binds to rest as a new list.
Why does Python's match statement need no break at the end of each case?
basics
~10 sPython's match statement has no fall-through. The first case whose pattern matches runs its block, and control then jumps past the whole statement, so there is nothing left to break out of.