0
0
Pythonprogramming~20 mins

Parameters and arguments in Python - Practice Problems & Coding Challenges

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

print(greet("Alice"))
print(greet("Bob", greeting="Hi"))
ATypeError: greet() missing 1 required positional argument: 'greeting'
BHello, Alice!\nHello, Bob!
CHi, Alice!\nHi, Bob!
DHello, Alice!\nHi, Bob!
Attempts:
2 left
๐Ÿ’ก Hint
Remember the default value is used if no argument is given for that parameter.
โ“ Predict Output
intermediate
2:00remaining
Output with *args and **kwargs parameters
What will this code print?
Python
def show_args(*args, **kwargs):
    print(f"Positional: {args}")
    print(f"Keyword: {kwargs}")

show_args(1, 2, a=3, b=4)
APositional: [1, 2]\nKeyword: {'a': 3, 'b': 4}
BPositional: (1, 2)\nKeyword: {'a': 3, 'b': 4}
CPositional: (1, 2)\nKeyword: [('a', 3), ('b', 4)]
DTypeError: show_args() got multiple values for argument 'a'
Attempts:
2 left
๐Ÿ’ก Hint
*args collects positional arguments as a tuple, **kwargs collects keyword arguments as a dictionary.
โ“ Predict Output
advanced
2:00remaining
Output of function with mutable default argument
What is the output of this code?
Python
def add_item(item, item_list=[]):
    item_list.append(item)
    return item_list

print(add_item('apple'))
print(add_item('banana'))
A['apple']\n['banana']
B['apple']\n['apple']
C['apple']\n['apple', 'banana']
DTypeError: unhashable type: 'list'
Attempts:
2 left
๐Ÿ’ก Hint
Default arguments are evaluated once when the function is defined, not each time it is called.
โ“ Predict Output
advanced
2:00remaining
Output of function with positional-only and keyword-only parameters
What will this code print?
Python
def func(a, /, b, *, c):
    return a + b + c

print(func(1, 2, c=3))
A6
BTypeError: func() missing 1 required keyword-only argument: 'c'
CTypeError: func() got some positional-only arguments passed as keyword arguments
DTypeError: func() missing 1 required positional argument: 'b'
Attempts:
2 left
๐Ÿ’ก Hint
Parameters before / are positional-only, parameters after * are keyword-only.
๐Ÿง  Conceptual
expert
2:00remaining
Identify the error in function call with argument unpacking
Given the function definition below, which option will cause a TypeError when called?
Python
def combine(a, b, c):
    return a + b + c
A
kwargs = {'a': 1, 'b': 2, 'c': 3}
combine(**kwargs, c=4)
B
args = (1, 2, 3)
combine(*args)
C
kwargs = {'a': 1, 'b': 2}
combine(**kwargs, c=3)
D
args = (1, 2)
combine(*args, 3)
Attempts:
2 left
๐Ÿ’ก Hint
Check if any argument is passed twice due to unpacking and explicit argument.