Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Arbitrary Keyword Arguments in Python | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Functions

bookArbitrary Keyword Arguments in Python

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
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.

  • The function filter_products_by_budget takes a budget and any number of named product–price pairs using **kwargs.
  • Inside the loop, use kwargs.items() to get each product name and its price.
  • Compare the values and check if budget is greater than or equal to price.
  • When the condition is true, store the product and its price in the affordable_products dictionary.
  • If the dictionary stays empty, return "No products available within the budget.".
  • If at least one product is added, return "Available products within budget: {affordable_products}".

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 13
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookArbitrary Keyword Arguments in Python

Swipe to show menu

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
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.

  • The function filter_products_by_budget takes a budget and any number of named product–price pairs using **kwargs.
  • Inside the loop, use kwargs.items() to get each product name and its price.
  • Compare the values and check if budget is greater than or equal to price.
  • When the condition is true, store the product and its price in the affordable_products dictionary.
  • If the dictionary stays empty, return "No products available within the budget.".
  • If at least one product is added, return "Available products within budget: {affordable_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 1. Chapter 13
single

single

some-alt