Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Combination of Positional and Keyword Arbitrary Arguments | Arbitrary Arguments
Python Functions Tutorial
Section 3. Chapter 3
single

single

bookCombination of Positional and Keyword Arbitrary Arguments

Swipe to show menu

Consider a function that calculates the total cost of multiple items while allowing you to pass any number of prices and optional keyword settings such as discounts or taxes.

12345678910111213141516171819202122232425
def calculate_total_cost(*prices, **settings): subtotal = sum(prices) discount = settings.get("discount", 0) tax = settings.get("tax", 0) discount_amount = subtotal * (discount / 100) taxed_amount = (subtotal - discount_amount) * (1 + tax / 100) print(f"Subtotal: ${subtotal:.2f}") if discount > 0: print(f"Discount: {discount}% (-${discount_amount:.2f})") else: print("No discount applied.") if tax > 0: print(f"Tax: {tax}% (+${taxed_amount - (subtotal - discount_amount):.2f})") print(f"Final total: ${taxed_amount:.2f}") print() # Examples of using the function calculate_total_cost(1000, 250, 50) calculate_total_cost(1200, 800, discount=10) calculate_total_cost(500, 750, 250, discount=5, tax=8)
copy

Rules for Combining Arbitrary Arguments

Positional Arbitrary Arguments (*args)

*args collects all extra positional arguments into a tuple. In this example, the function can take any number of item prices without needing to define them explicitly.

For example:

calculate_total_cost(500, 250, 100)

Here, *prices becomes (500, 250, 100).

Keyword Arbitrary Arguments (**kwargs)

**kwargs collects all named (keyword) arguments into a dictionary. This allows the function to accept additional named settings such as discount or tax, even if they weren’t predefined.

For example:

calculate_total_cost(1000, 500, discount=10, tax=5)

Here, **settings becomes {'discount': 10, 'tax': 5}.

Combining Both

You can use both *args and **kwargs in the same function to achieve maximum flexibility — letting you handle any number of positional values and any combination of named settings.

Note
Note

Always place *args before **kwargs in the function signature – reversing the order will result in a SyntaxError.

Task

Swipe to start coding

Implement a generate_report function that accepts any number of numeric scores and optional keyword settings.

  1. Use *args to collect scores (name the parameter scores) and **kwargs to collect optional settings.
  2. Calculate the average of all provided scores.
  3. If a "label" key is present in **kwargs, use its value as the report title; otherwise use "Report".
  4. If a "passing_score" key is present in **kwargs, check whether the average meets or exceeds it:
    • If yes, add "Status: Pass" to the report.
    • If no, add "Status: Fail".
  5. Return the result as a formatted string: "{label} | Average: {avg:.2f} | {status}". If no "passing_score" is provided, omit the status part: "{label} | Average: {avg:.2f}".

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 3
single

single

Ask AI

expand

Ask AI

ChatGPT

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

some-alt