0
0
Pythonprogramming~20 mins

Why different argument types are needed in Python - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Argument Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of function with positional 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
BHello, Alice!\nHello, Bob!
CHi, Alice!\nHi, Bob!
DHello, Alice!\nHi, Bob!
Attempts:
2 left
๐Ÿ’ก Hint

Check how default values work for keyword arguments.

โ“ Predict Output
intermediate
2:00remaining
Output of function with *args and **kwargs

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
Attempts:
2 left
๐Ÿ’ก Hint

Remember that *args collects positional arguments as a tuple, and **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, items=[]):
    items.append(item)
    return items

print(add_item('apple'))
print(add_item('banana'))
A['apple']\n['apple', 'banana']
B['apple']\n['banana']
C['apple']\n[]
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint

Think about how default mutable arguments behave in Python functions.

โ“ Predict Output
advanced
2:00remaining
Output of function with argument unpacking

What will this code print?

Python
def multiply(x, y, z):
    return x * y * z

values = (2, 3, 4)
print(multiply(*values))
A24
BTypeError
C9
DNone
Attempts:
2 left
๐Ÿ’ก Hint

Look at how the tuple is unpacked into function arguments.

โ“ Predict Output
expert
2:00remaining
Output of function with positional-only and keyword-only arguments

What is the output of this code?

Python
def func(a, b, /, c, d, *, e, f):
    return a + b + c + d + e + f

print(func(1, 2, 3, d=4, e=5, f=6))
A15
BTypeError
C21
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint

Understand how positional-only (/) and keyword-only (*) arguments work in Python 3.8+.