Home βΊ Problems βΊ Default parameter values
Default parameter values
π§ Predict the output Topic: functions Difficulty: medium Free
What this problem practises
Function bugs are mostly about names and values: positional argument order, default parameters evaluated once at definition time, local versus global scope, and the implicit None returned whenever a function has no return.
This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.
Difficulty: medium β expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.
The challenge
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The snippet
The interactive editor pre-fills exactly this code β read it, then submit your prediction:
def power(base, exponent=2):
return base ** exponent
print(power(3))
print(power(2, 3))
print(power(exponent=3, base=2))Understand the rule
Default parameter values fill in for arguments the caller omits: def greet(name, msg='Hello') lets greet('Sam') and greet('Sam', 'Hi') both work. The prediction rules: defaults are used only when the argument is absent, defaults are evaluated once at def time (which is why mutable defaults are their own famous trap), and parameters with defaults must come after those without.
How to trace it
- For each call, match the supplied arguments positionally, then by keyword.
- For every parameter still unfilled, substitute its default from the def line.
- Confirm no parameter without a default was left unfilled β that would be a TypeError, not output.
- Evaluate the body with the final bound values.
Common wrong fixes
- Thinking the default overrides a supplied argument, instead of yielding to it.
- Writing def f(a=1, b): β non-default after default is a SyntaxError before anything runs.
- Assuming the default expression re-evaluates each call; it is computed once when def executes.
Rule to remember
Defaults are fallbacks evaluated once at definition time and used only for missing arguments. Fill parameters in order β positional, keyword, then defaults β and the output follows.
π§© Learning path: Functions
Problem 5 of 12 in this path. Work them in order β each one builds on the rule before it.
β Previous problemNext in path: Function returning nothing β
πΌ Learning path: Interview Practice
Problem 14 of 16 in this path. Work them in order β each one builds on the rule before it.
β Set removes duplicates automaticallyNext in path: try-except-else flow β
More functions problems
Keep going
- Output-prediction strategies β how to predict tricky snippets without running them.
- Browse every functions problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.