Bird
Raised Fist0
Pythonprogramming~10 mins

Keyword arguments in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main advantage of using keyword arguments when calling a function in Python?
easy
A. Keyword arguments make the function run faster.
B. You must provide arguments in the exact order defined in the function.
C. You can specify arguments by name, making the code clearer.
D. They allow you to skip required arguments.

Solution

  1. Step 1: Understand keyword arguments

    Keyword arguments let you name the inputs when calling a function, so you don't have to remember the order.
  2. Step 2: Compare options

    You can specify arguments by name, making the code clearer. correctly states that naming arguments makes code clearer. Options B, C, and D are incorrect because keyword arguments do not require order, do not affect speed, and cannot skip required arguments.
  3. Final Answer:

    You can specify arguments by name, making the code clearer. -> Option C
  4. Quick Check:

    Keyword arguments = clearer code [OK]
Hint: Keyword arguments name inputs, so order doesn't matter [OK]
Common Mistakes:
  • Thinking keyword arguments must follow positional order
  • Believing keyword arguments speed up the function
  • Assuming keyword arguments can skip required parameters
2. Which of the following is the correct way to call the function def greet(name, age): using keyword arguments?
easy
A. greet(name='Alice', age=30)
B. greet('Alice', 30)
C. greet(age=30, 'Alice')
D. greet(name=30, age='Alice')

Solution

  1. Step 1: Check keyword argument syntax

    Keyword arguments require the format parameter=value. greet(name='Alice', age=30) uses name='Alice' and age=30, which is correct.
  2. Step 2: Identify errors in other options

    greet('Alice', 30) uses positional arguments only, which is valid syntax but does not use keyword arguments. greet(age=30, 'Alice') places positional argument after keyword, which is invalid syntax. greet(name=30, age='Alice') swaps types incorrectly.
  3. Final Answer:

    greet(name='Alice', age=30) -> Option A
  4. Quick Check:

    Keyword args = parameter=value pairs [OK]
Hint: Use parameter=value pairs for keyword arguments [OK]
Common Mistakes:
  • Placing positional arguments after keyword arguments
  • Swapping argument names and values
  • Mixing types incorrectly in keyword arguments
3. What will be the output of the following code?
def info(name, age):
    print(f"Name: {name}, Age: {age}")

info(age=25, name='Bob')
medium
A. Name: 25, Age: Bob
B. Name: Bob, Age: 25
C. Name: , Age:
D. TypeError

Solution

  1. Step 1: Understand keyword argument order

    Keyword arguments allow passing arguments in any order by naming them explicitly.
  2. Step 2: Match arguments to parameters

    Here, age=25 and name='Bob' are passed, so name gets 'Bob' and age gets 25.
  3. Final Answer:

    Name: Bob, Age: 25 -> Option B
  4. Quick Check:

    Keyword args reorder inputs correctly [OK]
Hint: Keyword arguments can be in any order [OK]
Common Mistakes:
  • Assuming order matters with keyword arguments
  • Confusing parameter names with values
  • Expecting errors when order is changed
4. Identify the error in the following function call:
def multiply(x, y):
    return x * y

result = multiply(x=5, 10)
medium
A. SyntaxError: positional argument after keyword argument
B. TypeError: missing required positional argument
C. No error, result is 50
D. NameError: variable not defined

Solution

  1. Step 1: Check argument order rules

    In Python, positional arguments cannot come after keyword arguments in a function call.
  2. Step 2: Analyze the call

    The call multiply(x=5, 10) has a keyword argument first, then a positional argument, which causes a syntax error.
  3. Final Answer:

    SyntaxError: positional argument after keyword argument -> Option A
  4. Quick Check:

    Positional args must come before keyword args [OK]
Hint: Positional arguments must come before keyword arguments [OK]
Common Mistakes:
  • Placing positional arguments after keyword arguments
  • Confusing error types for this syntax
  • Assuming this call runs without error
5. Given the function def order(item, quantity=1, price=10):, which call correctly orders 3 items with a price of 15 each using keyword arguments?
hard
A. order('apple', 3, 15)
B. order(price=15, quantity=3)
C. order(quantity=3, 'apple', price=15)
D. order(item='apple', price=15, quantity=3)

Solution

  1. Step 1: Understand function parameters and defaults

    The function has one required parameter item and two optional parameters quantity and price with defaults.
  2. Step 2: Check each call for correctness

    order('apple', 3, 15) uses positional arguments only, which is valid but not keyword arguments. order(item='apple', price=15, quantity=3) uses keyword arguments for all parameters, correctly naming them in any order. order(quantity=3, 'apple', price=15) places positional argument after keyword argument, which is invalid. order(price=15, quantity=3) misses the required item argument.
  3. Final Answer:

    order(item='apple', price=15, quantity=3) -> Option D
  4. Quick Check:

    Keyword args can be in any order, all required given [OK]
Hint: Use all required keywords, order doesn't matter [OK]
Common Mistakes:
  • Omitting required arguments
  • Placing positional after keyword arguments
  • Using positional only when keyword is asked