0
0
Pythonprogramming~10 mins

Keyword arguments 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 call the function with a keyword argument.

Python
def greet(name):
    print(f"Hello, {name}!")

greet([1]="Alice")
Drag options to blanks, or click blank then click option'
Aname
BAlice
Cprint
Dgreet
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the value instead of the parameter name as the keyword.
Forgetting to use the keyword argument and just passing a value.
2fill in blank
medium

Complete the function call to use keyword arguments for both parameters.

Python
def describe_pet(animal_type, pet_name):
    print(f"I have a {animal_type} named {pet_name}.")

describe_pet(animal_type=[1], pet_name="Buddy")
Drag options to blanks, or click blank then click option'
A"dog"
Bdog
Canimal_type
Dpet_name
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing the value without quotes, causing a NameError.
Using the parameter name as the value instead of a string.
3fill in blank
hard

Fix the error in the function call by using the correct keyword argument.

Python
def calculate_area(width, height):
    return width * height

area = calculate_area([1]=5, height=10)
Drag options to blanks, or click blank then click option'
Alength
Bwidth
Cheight
Darea
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a wrong keyword argument name like length.
Mixing up parameter names.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters and transforms items using keyword arguments.

Python
def filter_items(items, min_price):
    return {item: price for item, price in items.items() if price [1] min_price}

items = {'apple': 5, 'banana': 2, 'cherry': 7}
filtered = filter_items(items, min_price=[2])
Drag options to blanks, or click blank then click option'
A>
B3
C<
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the wrong comparison operator.
Passing a string instead of a number for min_price.
5fill in blank
hard

Fill all three blanks to define and call a function using keyword arguments with default values.

Python
def make_coffee(size=[1], sugar=[2], milk=[3]):
    print(f"Making a {size} coffee with {sugar} sugar and {milk} milk.")

make_coffee(size='large', sugar=2, milk='yes')
Drag options to blanks, or click blank then click option'
A'small'
B0
C'no'
D'medium'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using numbers as strings or vice versa.
Forgetting quotes around string default values.