skip to content

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 pageshow

explore

questions

65 · 3 sections

What is the difference between `break` and `continue` in a Python loop?

level: juniorimportance: must knowfreq 82%
basics
~20 s

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

open as a page

What does Python's enumerate() yield, and what does its start argument change?

level: juniorimportance: must knowfreq 70%
basics
~10 s

enumerate() 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.

open as a page

Why does calling list.remove inside a for loop over that same list skip elements?

level: juniorimportance: must knowfreq 76%
basics
~20 s

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

open as a page

When does the else block of a Python for or while loop actually run?

level: juniorimportance: must knowfreq 40%
basics
~20 s

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

open as a page

What does Python's range(10, 0, -1) produce, and when is a range empty?

level: juniorimportance: must knowfreq 72%
basics
~20 s

range(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).

open as a page

In Python's `a if cond else b`, what is evaluated and in what order?

level: juniorimportance: must knowfreq 70%
basics
~20 s

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

open as a page

Why does a Python dict dispatch table store `handler` rather than `handler()` as its values?

level: juniorimportance: must knowfreq 60%
basics
~20 s

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

open as a page

How does Python decide which branch of an if/elif/else chain executes?

level: juniorimportance: must knowfreq 80%
basics
~10 s

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

open as a page

What does Python's walrus operator `:=` do, and how does it differ from `=`?

level: juniorimportance: must knowfreq 65%
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.

open as a page

How does Python parse `'Result: ' + status if ok else 'fail'`, and why is that a bug?

level: middleimportance: must knowfreq 50%
basics
~20 s

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

open as a page

Why does `case RED:` in a Python match statement rebind RED instead of comparing against it?

level: juniorimportance: must knowfreq 50%
basics
~20 s

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

open as a page

What does `case _:` match in a Python match statement, and where must it appear?

level: juniorimportance: must knowfreq 55%
basics
~20 s

case _: 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.

open as a page

What does the `if` guard on a `match` statement's `case` clause do?

level: juniorimportance: must knowfreq 42%
basics
~20 s

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

open as a page

In a Python match statement, what does `case [a, b, *rest]` match, and what does it bind?

level: juniorimportance: must knowfreq 50%
basics
~20 s

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

open as a page

Why does Python's match statement need no break at the end of each case?

level: juniorimportance: must knowfreq 70%
basics
~10 s

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

open as a page