Arbitrary 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.
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))
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.
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.
123456789101112131415161718def 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])
As you can see from the results:
- When the function is called without any arguments,
argswill be an empty tuple(); - When the function is called with one argument,
argswill 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.
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.
- Use arbitrary arguments named prices in the
calculate_totalfunction. - If no arguments are provided, return
"Your cart is empty.". - Apply a 20% discount if the total is $200 or more.
- Apply a 10% discount if the total is $100 or more.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.35
Arbitrary 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.
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))
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.
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.
123456789101112131415161718def 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])
As you can see from the results:
- When the function is called without any arguments,
argswill be an empty tuple(); - When the function is called with one argument,
argswill 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.
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.
- Use arbitrary arguments named prices in the
calculate_totalfunction. - If no arguments are provided, return
"Your cart is empty.". - Apply a 20% discount if the total is $200 or more.
- Apply a 10% discount if the total is $100 or more.
Solution
Thanks for your feedback!
single