Python Control Flow Quiz: Loops, Range, Zip and Match

Test your Python control flow with 12 questions and three programs on if and elif, while and for loops, range, enumerate, zip, match and loop patterns.

  • Course: Python study plan
  • Module: Control flow
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 22 min
  • Runtime: CPython 3.11

Checkpoint — Control flow is the checkpoint that closes the Control flow module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the whole module: if/elif/else and the guard-clause shape, while with break, continue and the loop else, for over any iterable with range, enumerate and zip, structural pattern matching with match, and the loop patterns — accumulate, search, running state, two pointers, prefix sums and building output.

How it works. Twelve questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.

Before you start, make sure you can answer these from memory:

  • What is a guard clause, and what does it do to the indentation of the rest of the function?
  • When does the else of a while or for loop run, and when does it not?
  • What does range(2, 10, 3) produce, and why is range(len(xs)) exactly the valid indices?
  • What does zip do when its inputs have different lengths, and how do you make that an error?
  • In a case pattern, what is the difference between RED and Colour.RED?
  • Why must a list not be modified while a for loop iterates over it?
  • Which built-ins replace the accumulate, count and extreme loops?

The three programs are a FizzBuzz with configurable divisors, a prime lister built on a trial-division loop with for … else, and a bank account whose commands are dispatched with match.

Common questions

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

It runs when the loop finishes normally — the condition became false or the iterable ran out — and not when the loop is left by break. That makes it the "not found" branch of a search, such as a prime test that breaks on the first divisor it finds.

What does range(2, 10, 3) produce?

The integers 2, 5 and 8: it starts at 2, steps by 3 and stops before 10, because stop is excluded. By the same half-open rule, range(len(xs)) is exactly the valid indices of xs, 0 to len(xs) - 1.

What does zip do when its inputs have different lengths?

It stops at the shortest input without any warning. Pass strict=True (Python 3.10 and later) to make a length mismatch raise ValueError, or use itertools.zip_longest to pad the shorter input instead.

Exercises

FizzBuzz, configured

Print the numbers from 1 to n, except that multiples of a print Fizz, multiples of b print Buzz, and multiples of both print FizzBuzz.

Input: one line n a b with n >= 1 and a, b >= 1. Output: n lines.

6 2 3

prints

1
Fizz
Buzz
Fizz
5
FizzBuzz

Primes up to n

List every prime up to and including n. Test each candidate by trial division in a for loop over the possible divisors from 2 up to its square root, using the loop's else clause to record that no divisor was found.

Input: one integer n >= 0. Output: primes: <space-separated primes> (or primes: none), then count: <k>.

10

prints

primes: 2 3 5 7
count: 4

Bank account

Run a bank account from commands read until the end of input, dispatching each with match on line.split(). deposit n adds n and prints ok <balance>; withdraw n subtracts n and prints ok <balance>, or insufficient funds if the balance would go negative; balance prints balance <balance>. A deposit or withdrawal whose amount is not positive prints invalid amount (use a guard). Anything else prints unknown: <line>. Amounts are integers.

Input: commands, one per line. Output: one line per command.

deposit 100
withdraw 30
balance
withdraw 100
deposit -5
foo

prints

ok 100
ok 70
balance 70
insufficient funds
invalid amount
unknown: foo

In this module: Control flow

← The loop patterns · Defining functions — def, return and functions as values →