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

bookArbitrary Positional Arguments

You already know positional and optional arguments. But when a function may receive many inputs or you don’t know them in advance, you can use arbitrary positional arguments. They allow a function to accept any number of values.

Note
Note

Each argument can be any data structure (list, dict, etc.). Arbitrary arguments let you pass as many such objects as needed.

To define arbitrary positional arguments, place an asterisk * before the parameter name. Example:

12345678
# Define function with arbitrary positional arguments named values def calculate_sum(*values): return sum(values) # Test the function using different number of arguments print(calculate_sum(1, 2, 3)) print(calculate_sum(1, 2, 3, 4)) print(calculate_sum(1, 2, 3, 4, 5))
copy

Here, *values collects all passed positional arguments into a tuple. Inside the function, you use the variable name without *. The result is correct regardless of how many arguments are provided.

Note
Note

Although any name works, the common and readable form is *args.

1234567891011121314
def example_function(*args): print(type(args)) print(args) for arg in args: print(arg) print("Call without arguments:") example_function() print("\nCall with one argument:") example_function(1) print("\nCall with multiple arguments:") example_function(1, 2, 3, 'hello', [4, 5, 6])
copy

As shown:

  • No arguments β†’ args is ();
  • One argument β†’ (1,);
  • Multiple arguments β†’ all values appear in a tuple, e.g., (1, 2, 3, 'hello', [4, 5, 6]).

*args behaves like any other tuple, giving you full flexibility when handling many inputs.

Task

Swipe to start coding

Implement a calculate_total function that calculates the total price of items in a cart, applying discounts based on the total amount.

  1. Use arbitrary positional arguments named prices in the calculate_total function.
  2. If no arguments are provided, return "Your cart is empty.".
  3. Apply a 20% discount if the total is $200 or more.
  4. Apply a 10% discount if the total is $100 or more.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

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

close

bookArbitrary Positional Arguments

Swipe to show menu

You already know positional and optional arguments. But when a function may receive many inputs or you don’t know them in advance, you can use arbitrary positional arguments. They allow a function to accept any number of values.

Note
Note

Each argument can be any data structure (list, dict, etc.). Arbitrary arguments let you pass as many such objects as needed.

To define arbitrary positional arguments, place an asterisk * before the parameter name. Example:

12345678
# Define function with arbitrary positional arguments named values def calculate_sum(*values): return sum(values) # Test the function using different number of arguments print(calculate_sum(1, 2, 3)) print(calculate_sum(1, 2, 3, 4)) print(calculate_sum(1, 2, 3, 4, 5))
copy

Here, *values collects all passed positional arguments into a tuple. Inside the function, you use the variable name without *. The result is correct regardless of how many arguments are provided.

Note
Note

Although any name works, the common and readable form is *args.

1234567891011121314
def example_function(*args): print(type(args)) print(args) for arg in args: print(arg) print("Call without arguments:") example_function() print("\nCall with one argument:") example_function(1) print("\nCall with multiple arguments:") example_function(1, 2, 3, 'hello', [4, 5, 6])
copy

As shown:

  • No arguments β†’ args is ();
  • One argument β†’ (1,);
  • Multiple arguments β†’ all values appear in a tuple, e.g., (1, 2, 3, 'hello', [4, 5, 6]).

*args behaves like any other tuple, giving you full flexibility when handling many inputs.

Task

Swipe to start coding

Implement a calculate_total function that calculates the total price of items in a cart, applying discounts based on the total amount.

  1. Use arbitrary positional arguments named prices in the calculate_total function.
  2. If no arguments are provided, return "Your cart is empty.".
  3. Apply a 20% discount if the total is $200 or more.
  4. Apply a 10% discount if the total is $100 or more.

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Β 1
single

single

some-alt