0
0
Pythonprogramming~20 mins

Argument order rules in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Argument Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of function call with mixed arguments
What is the output of this Python code?
Python
def greet(name, greeting="Hello", punctuation="!"):
    return f"{greeting}, {name}{punctuation}"

print(greet("Alice", punctuation="."))
ATypeError: positional argument follows keyword argument
BHello, Alice!
CHello, Alice.
DHello, punctuation=., Alice
Attempts:
2 left
๐Ÿ’ก Hint
Remember that positional arguments come before keyword arguments.
โ“ Predict Output
intermediate
2:00remaining
Error from wrong argument order
What error does this code produce?
Python
def add(x, y=5, z):
    return x + y + z

add(1, 2, 3)
A6
BNameError: name 'z' is not defined
CSyntaxError: invalid syntax
DSyntaxError: non-default argument follows default argument
Attempts:
2 left
๐Ÿ’ก Hint
Check the order of parameters with and without default values.
โ“ Predict Output
advanced
2:00remaining
Output with *args and keyword-only arguments
What is the output of this code?
Python
def func(a, b, *args, c=10, d=20):
    return a + b + sum(args) + c + d

print(func(1, 2, 3, 4, c=5))
A35
B40
CTypeError: func() missing 1 required keyword-only argument: 'd'
D30
Attempts:
2 left
๐Ÿ’ก Hint
Remember that after *args, parameters are keyword-only.
โ“ Predict Output
advanced
2:00remaining
Output with positional-only and keyword-only arguments
What is the output of this code?
Python
def example(a, /, b, *, c):
    return a + b + c

print(example(1, 2, c=3))
A6
BTypeError: example() missing 1 required positional argument: 'b'
CTypeError: example() got some positional-only arguments passed as keyword arguments
D5
Attempts:
2 left
๐Ÿ’ก Hint
Positional-only arguments must be passed positionally, keyword-only must be passed by name.
๐Ÿง  Conceptual
expert
2:00remaining
Identify the invalid function definition
Which of these function definitions is invalid due to argument order rules?
Adef f(a, b=2, *, c, d=4): pass
Bdef f(a=1, b, c=3): pass
Cdef f(a, /, b, *, c=3): pass
Ddef f(*args, b, c=3): pass
Attempts:
2 left
๐Ÿ’ก Hint
Parameters with default values must come after those without defaults.