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

bookArbitrary Arguments

You have already explored positional and optional arguments. But what if a function has many arguments or you do not know all of them in advance? You can handle this using arbitrary arguments (variable-length arguments). These arguments let a function accept any number of inputs, making it flexible and adaptable.

Note
Note

Remember that each argument can be a data structure, such as a list or dictionary. You can pass an arbitrary number of such structures using arbitrary arguments.

In Python, you can define arbitrary arguments as a single positional argument by placing an asterisk * before the argument name. Here's an example:

12345678
# Define function with arbitrary 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

In the code above, the * indicates that the values variable can contain multiple arbitrary arguments, which are treated as a tuple. The built-in sum() function is then used to calculate the total. The result is correct for all examples, and you do not need to specify the number of arguments explicitly. Note: use this argument by name without * inside the function body.

Note
Note

To define an arbitrary argument tuple, it's enough to use * before the argument name. However, it is recommended to use the *args construction. This improves the interpretability and readability of the code.

123456789101112131415161718
def example_function(*args): # Print the type of args print(f'Type of args: {type(args)}') # Print the whole tuple print(f'Args tuple: {args}') # Iterate over the tuple for arg in args: print(arg) # Call the function without any arguments print("Call without arguments:") example_function() # Call the function with one argument print("\nCall with one argument:") example_function(1) # Call the function with multiple arguments print("\nCall with multiple arguments:") example_function(1, 2, 3, 'hello', [4, 5, 6])
copy

As you can see from the results:

  • When the function is called without any arguments, args will be an empty tuple ();
  • When the function is called with one argument, args will be a tuple with one element (1,);
  • When the function is called with multiple arguments, all of them are stored in the tuple, for example (1, 2, 3, 'hello', [4, 5, 6]).

With *args, you can work with the values just like you would with any other tuple in Python.

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

Awesome!

Completion rate improved to 4.35

bookArbitrary Arguments

Swipe to show menu

You have already explored positional and optional arguments. But what if a function has many arguments or you do not know all of them in advance? You can handle this using arbitrary arguments (variable-length arguments). These arguments let a function accept any number of inputs, making it flexible and adaptable.

Note
Note

Remember that each argument can be a data structure, such as a list or dictionary. You can pass an arbitrary number of such structures using arbitrary arguments.

In Python, you can define arbitrary arguments as a single positional argument by placing an asterisk * before the argument name. Here's an example:

12345678
# Define function with arbitrary 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

In the code above, the * indicates that the values variable can contain multiple arbitrary arguments, which are treated as a tuple. The built-in sum() function is then used to calculate the total. The result is correct for all examples, and you do not need to specify the number of arguments explicitly. Note: use this argument by name without * inside the function body.

Note
Note

To define an arbitrary argument tuple, it's enough to use * before the argument name. However, it is recommended to use the *args construction. This improves the interpretability and readability of the code.

123456789101112131415161718
def example_function(*args): # Print the type of args print(f'Type of args: {type(args)}') # Print the whole tuple print(f'Args tuple: {args}') # Iterate over the tuple for arg in args: print(arg) # Call the function without any arguments print("Call without arguments:") example_function() # Call the function with one argument print("\nCall with one argument:") example_function(1) # Call the function with multiple arguments print("\nCall with multiple arguments:") example_function(1, 2, 3, 'hello', [4, 5, 6])
copy

As you can see from the results:

  • When the function is called without any arguments, args will be an empty tuple ();
  • When the function is called with one argument, args will be a tuple with one element (1,);
  • When the function is called with multiple arguments, all of them are stored in the tuple, for example (1, 2, 3, 'hello', [4, 5, 6]).

With *args, you can work with the values just like you would with any other tuple in Python.

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