0
0
Pythonprogramming~20 mins

Keyword arguments in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Keyword Arguments 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 keyword arguments
What is the output of this Python code?
Python
def greet(name, greeting="Hello", punctuation="!"):
    return f"{greeting}, {name}{punctuation}"

print(greet(name="Alice", punctuation=".", greeting="Hi"))
A"Hi, Alice!"
B"Hello, Alice!"
C"Hi, Alice."
D"Hello, Alice."
Attempts:
2 left
๐Ÿ’ก Hint
Check how keyword arguments match the function parameters regardless of order.
โ“ Predict Output
intermediate
2:00remaining
Function call with missing keyword argument
What is the output of this code snippet?
Python
def calculate_area(length, width=5):
    return length * width

print(calculate_area(width=3, length=4))
A20
B12
CTypeError
D15
Attempts:
2 left
๐Ÿ’ก Hint
Keyword arguments can be passed in any order, but all required arguments must be provided.
โ“ Predict Output
advanced
2:00remaining
Output when mixing positional and keyword arguments
What will this code print?
Python
def describe_pet(pet_name, animal_type="dog"):
    return f"I have a {animal_type} named {pet_name}."

print(describe_pet("Whiskers", animal_type="cat"))
ATypeError
B"I have a dog named Whiskers."
C"I have a cat named animal_type."
D"I have a cat named Whiskers."
Attempts:
2 left
๐Ÿ’ก Hint
Positional arguments come first, then keyword arguments can override defaults.
โ“ Predict Output
advanced
2:00remaining
Error caused by duplicate argument assignment
What error does this code raise?
Python
def multiply(x, y):
    return x * y

print(multiply(2, x=3))
ATypeError: multiply() got multiple values for argument 'x'
BTypeError: multiply() missing 1 required positional argument: 'y'
CSyntaxError
D6
Attempts:
2 left
๐Ÿ’ก Hint
Check if the same argument is given twice by position and keyword.
๐Ÿง  Conceptual
expert
2:00remaining
Understanding **kwargs behavior
What is the output of this code?
Python
def show_info(**kwargs):
    return kwargs.get('name', 'Unknown') + ' is ' + str(kwargs.get('age', 'ageless'))

print(show_info(age=30, name='Bob'))
A"Bob is 30"
BTypeError
C"Bob is ageless"
D"Unknown is 30"
Attempts:
2 left
๐Ÿ’ก Hint
**kwargs collects keyword arguments into a dictionary.