The rules, again
Same game as chapter 1: read the question, commit to a prediction, run it, and only then open the answer. Wrong prediction means the drill goes on your redo list for three days from now.
This chapter contains the single most famous gotcha in the language, the mutable default argument. It also contains the idea that unlocks half the series: functions are objects, defaults are evaluated once, and the mutate-vs-rebind distinction from chapter 1 decides whether the caller sees your changes.
Rusty on the basics? Work through Functions and Multiple Function Arguments on learnpython.org first, then come back and drill.
One idea before you start
Two drills in this chapter crash on scope, so know the lookup order going in.
The drills
Click a question to open it. The answer stays hidden until you ask for it. Predict first.
Want more drills?
When these stop surprising you, let an AI generate fresh ones. Copy this prompt into ChatGPT, Claude, or any assistant you like:
You are my Python drill coach. Quiz me on: functions, *args and
**kwargs, argument unpacking, keyword-only arguments, default
argument evaluation, mutate vs rebind on parameters, LEGB scope,
lambdas, and functions as first-class objects.
Rules:
- Ask ONE question at a time. Show a short code snippet and ask me to
predict the output (or the error) before running it.
- Wait for my answer. Do not reveal anything until I reply.
- If I am right, say so in one line and raise the difficulty slightly.
- If I am wrong, explain the why in 2-3 sentences, then give me a
similar question later to check I really got it.
- Prefer tricky edge cases over textbook questions: def f(x, y=[]),
defaults calling datetime.now(), count += 1 on a global,
def f(a, *, b), rect(*size, **style), shadowing built-ins like list.
- Keep score. After 10 questions, list the concepts I missed so I can
add them to my redo list.
Start with a medium question.The predict-first rule matters more than the tool. Whatever generates the questions, always commit to an answer before you look.
Done?
Score yourself honestly and queue up your misses. Drills 9 and 10 are the pair to sit with: same-looking code, opposite outcomes, and the mutate-vs-rebind distinction between them is what interviewers are really probing when they ask "is Python pass-by-value or pass-by-reference?" (Answer: neither, it passes object references by value.)
Next chapter: comprehensions and generators, where one character of difference decides whether your data fits in memory.