Bird
Raised Fist0
Pythonprogramming~20 mins

Default arguments in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Default Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of function with mutable default argument
What is the output of this code when calling append_number() three times in a row?
Python
def append_number(num, lst=[]):
    lst.append(num)
    return lst

print(append_number(1))
print(append_number(2))
print(append_number(3))
A
[1]
[2]
[3]
BTypeError
C
[1]
[1]
[1]
D
[1]
[1, 2]
[1, 2, 3]
Attempts:
2 left
๐Ÿ’ก Hint
Think about how default arguments are evaluated once when the function is defined.
โ“ Predict Output
intermediate
1:30remaining
Output with default argument and keyword argument
What will this code print?
Python
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", greeting="Hi"))
AHello, Alice!\nHi, Bob!
BHello, Alice!\nHello, Bob!
CHi, Alice!\nHi, Bob!
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Check how the default argument is overridden by the keyword argument.
โ“ Predict Output
advanced
2:30remaining
Default argument evaluated once
What is the output of this code?
Python
def counter(start=0):
    def inner():
        nonlocal start
        start += 1
        return start
    return inner

c1 = counter()
c2 = counter(10)
print(c1())
print(c1())
print(c2())
print(c2())
A
0
1
10
11
B
1
1
11
11
C
1
2
11
12
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Each call to counter creates a new closure with its own start value.
๐Ÿ”ง Debug
advanced
1:30remaining
Identify the error with default argument
What error does this code raise when called as add_item(5)?
Python
def add_item(item, items=None):
    items.append(item)
    return items

add_item(5)
AAttributeError
BTypeError
CNameError
DNo error, returns [5]
Attempts:
2 left
๐Ÿ’ก Hint
Check what happens when items is None and you try to call append.
๐Ÿš€ Application
expert
2:30remaining
Predict the dictionary content after function calls
Consider this function and calls. What is the content of data after all calls?
Python
def add_entry(key, value, data={}):
    data[key] = value
    return data

data = add_entry('a', 1)
data = add_entry('b', 2)
data = add_entry('a', 3)
A{'a': 1, 'b': 2, 'a': 3}
B{'a': 3, 'b': 2}
C{'a': 1, 'b': 2}
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Remember that the default dictionary is shared across calls.

Practice

(1/5)
1. What is the main purpose of default arguments in Python functions?
easy
A. To create variables inside the function
B. To provide preset values for parameters if no argument is given
C. To make functions run faster
D. To force the user to always provide all arguments

Solution

  1. Step 1: Understand default arguments

    Default arguments allow a function to have parameters with preset values if the caller does not provide them.
  2. Step 2: Identify the purpose

    This makes the function easier to call without needing all arguments every time.
  3. Final Answer:

    To provide preset values for parameters if no argument is given -> Option B
  4. Quick Check:

    Default arguments = preset values [OK]
Hint: Default arguments fill in missing inputs automatically [OK]
Common Mistakes:
  • Thinking default arguments speed up code
  • Believing default arguments force all inputs
  • Confusing default arguments with variable creation
2. Which of the following function definitions uses default arguments correctly?
easy
A. def greet(name='Friend', age):
B. def greet(name=, age=30):
C. def greet(name, age=30):
D. def greet(name, age=):

Solution

  1. Step 1: Check default argument placement

    Parameters with default values must come after parameters without defaults.
  2. Step 2: Analyze each option

    def greet(name='Friend', age): places a default before a non-default parameter, which is invalid syntax. Options C and D have syntax errors. def greet(name, age=30): correctly places the default argument after a required parameter.
  3. Final Answer:

    def greet(name, age=30): -> Option C
  4. Quick Check:

    Defaults after required params = correct syntax [OK]
Hint: Put default parameters after required ones [OK]
Common Mistakes:
  • Placing default arguments before required ones
  • Leaving default values empty
  • Using invalid syntax for defaults
3. What is the output of this code?
def multiply(a, b=2):
    return a * b

print(multiply(4))
print(multiply(4, 3))
medium
A. Error
B. 6 and 12
C. 8 and 6
D. 8 and 12

Solution

  1. Step 1: Understand default argument usage

    When multiply(4) is called, b uses its default value 2, so 4 * 2 = 8.
  2. Step 2: Evaluate second call

    When multiply(4, 3) is called, b is 3, so 4 * 3 = 12.
  3. Final Answer:

    8 and 12 -> Option D
  4. Quick Check:

    Default used first call, explicit second call = 8 and 12 [OK]
Hint: Default used if argument missing, else explicit value [OK]
Common Mistakes:
  • Assuming default is ignored when argument is given
  • Confusing order of arguments
  • Expecting error when default is used
4. Find the error in this function definition:
def add_numbers(x=5, y):
    return x + y
medium
A. Default argument before non-default argument causes SyntaxError
B. Missing return statement
C. Function name is invalid
D. No error, code runs fine

Solution

  1. Step 1: Check parameter order

    Parameters with default values must come after parameters without defaults in Python.
  2. Step 2: Identify error

    Here, x has a default but y does not, so this causes a SyntaxError.
  3. Final Answer:

    Default argument before non-default argument causes SyntaxError -> Option A
  4. Quick Check:

    Default before required param = SyntaxError [OK]
Hint: Non-default params must come before default ones [OK]
Common Mistakes:
  • Ignoring parameter order rules
  • Thinking missing return causes error here
  • Assuming function name is invalid
5. You want a function that greets a user with their name and an optional greeting word (default is 'Hello'). Which function definition is correct and prints "Hi, Alice!" when called as greet('Alice', 'Hi') and "Hello, Bob!" when called as greet('Bob')?
hard
A. def greet(name, greeting='Hello'): print(f"{greeting}, {name}!")
B. def greet(greeting='Hello', name): print(f"{greeting}, {name}!")
C. def greet(name, greeting): print(f"{greeting}, {name}!")
D. def greet(name='User', greeting='Hello'): print(f"{name}, {greeting}!")

Solution

  1. Step 1: Check parameter order and defaults

    Parameters with defaults must come after those without. def greet(name, greeting='Hello'): print(f"{greeting}, {name}!") has name first (no default), greeting second (default 'Hello').
  2. Step 2: Verify behavior

    Calling greet('Alice', 'Hi') prints "Hi, Alice!". Calling greet('Bob') uses default greeting "Hello" and prints "Hello, Bob!".
  3. Final Answer:

    def greet(name, greeting='Hello'): print(f"{greeting}, {name}!") -> Option A
  4. Quick Check:

    Defaults after required params, works as expected [OK]
Hint: Put required params first, defaults after [OK]
Common Mistakes:
  • Placing default param before required param
  • Not providing default for optional greeting
  • Using default for name causing unexpected calls