0
0
Pythonprogramming~10 mins

Variable-length keyword arguments (**kwargs) in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that accepts any number of keyword arguments.

Python
def greet([1]):
    for key, value in kwargs.items():
        print(f"{key}: {value}")
Drag options to blanks, or click blank then click option'
A*args
B*kwargs
Ckwargs
D**kwargs
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using *args instead of **kwargs
Forgetting the ** before kwargs
Using kwargs without ** which is just a normal parameter
2fill in blank
medium

Complete the code to call the function with keyword arguments.

Python
def show_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

show_info([1])
Drag options to blanks, or click blank then click option'
A['name', 'Alice', 'age', 30]
B'name', 'Alice', 'age', 30
Cname='Alice', age=30
D(name='Alice', age=30)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing arguments as a list or tuple
Passing arguments as strings instead of key=value pairs
Using parentheses around arguments incorrectly
3fill in blank
hard

Fix the error in the function definition to correctly accept keyword arguments.

Python
def details([1]):
    for key, value in kwargs.items():
        print(f"{key}: {value}")
Drag options to blanks, or click blank then click option'
A**kwargs
Bkwargs
C*args
D*kwargs
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using single asterisk instead of double
Not using any asterisk
Using *args instead of **kwargs
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters kwargs with values greater than 10.

Python
filtered = {key: value for key, value in kwargs.items() if value [1] 10 and isinstance(value, [2])}
Drag options to blanks, or click blank then click option'
A>
Bint
Cstr
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using less than operator instead of greater than
Checking for string type instead of int
Forgetting to check the type
5fill in blank
hard

Fill all three blanks to create a function that prints keys in uppercase and values if they are strings.

Python
def print_strings([1]):
    for key, value in [2].items():
        if isinstance(value, [3]):
            print(f"{key.upper()}: {value}")
Drag options to blanks, or click blank then click option'
A**kwargs
Bkwargs
Cstr
D*args
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using *args instead of **kwargs
Not iterating over kwargs
Checking for wrong type