Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Arbitrary Keyword Arguments | Arbitrary Arguments
Python Functions Tutorial
course content

Course Content

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
Arbitrary Keyword Arguments

In programming, there is a special syntax for passing any number of named parameters to a function, known as **kwargs.

**kwargs allows a function to accept any number of named arguments and treat them as a dictionary.

123456
def example_function(**kwargs): for key, value in kwargs.items(): print(f'{key}: {value}') # Example function call example_function(name='John', age=25, city='New York')
copy

In this example, **kwargs receives named arguments and prints their keys and values.

Note

The .items() method is used to obtain a list of key-value pairs from a dictionary in Python. Each element in this list is represented as a tuple (key, value).

Task

Swipe to start coding

Implement a function that filters products based on a given budget. The function should return a list of affordable products or indicate if no products are available within the budget.

  1. You’re given the function filter_products_by_budget with input parameters budget and arbitrary keyword arguments (kwargs).
  2. In the for loop, use the items() method on kwargs to retrieve the product and price variables.
  3. Check if the budget variable is greater than or equal to the price variable.
  4. If the condition is met, add the product to the affordable_products dictionary, where the key is the product variable and the value is the price variable.
  5. If no products are within the budget, return the message "No products available within the budget."
  6. If at least one affordable product is found, Return the message "Available products within budget: {affordable_products}", where {affordable_products} is the dictionary of available products.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

book
Arbitrary Keyword Arguments

In programming, there is a special syntax for passing any number of named parameters to a function, known as **kwargs.

**kwargs allows a function to accept any number of named arguments and treat them as a dictionary.

123456
def example_function(**kwargs): for key, value in kwargs.items(): print(f'{key}: {value}') # Example function call example_function(name='John', age=25, city='New York')
copy

In this example, **kwargs receives named arguments and prints their keys and values.

Note

The .items() method is used to obtain a list of key-value pairs from a dictionary in Python. Each element in this list is represented as a tuple (key, value).

Task

Swipe to start coding

Implement a function that filters products based on a given budget. The function should return a list of affordable products or indicate if no products are available within the budget.

  1. You’re given the function filter_products_by_budget with input parameters budget and arbitrary keyword arguments (kwargs).
  2. In the for loop, use the items() method on kwargs to retrieve the product and price variables.
  3. Check if the budget variable is greater than or equal to the price variable.
  4. If the condition is met, add the product to the affordable_products dictionary, where the key is the product variable and the value is the price variable.
  5. If no products are within the budget, return the message "No products available within the budget."
  6. If at least one affordable product is found, Return the message "Available products within budget: {affordable_products}", where {affordable_products} is the dictionary of available products.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt