single
Arbitrary 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.
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))
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.
Although any name works, the common and readable form is *args.
1234567891011121314def 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])
As shown:
- No arguments →
argsis(); - 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.
Since *args is just a tuple, you can apply any logic to its values – including conditionals. For example, after summing all prices, you can check the total and apply different rules depending on the result.
Handling Empty Arguments
Because *args gathers inputs into a tuple, there is a chance a user might call your function without passing any arguments at all (example_function()). In this case, args will be an empty tuple: ().
It is a best practice to test whether *args is empty before processing data. You can do this easily because empty tuples are considered "falsy" in Python.
Here are two common ways to handle an empty *args tuple.
Using if not args
You can use if not args to detect an empty tuple and handle it immediately, such as by returning 0 or a custom message.
1234567891011def calculate_average(*prices): # Checking if the tuple is empty if not prices: return "No prices provided!" # If it's not empty, proceed with the calculation return sum(prices) / len(prices) # Testing the function print(calculate_average(10, 20, 30)) # Output: 20.0 print(calculate_average()) # Output: No prices provided!
Checking the Length with len()
Alternatively, you can check if the total number of arguments is equal to zero using the len() function:
12345678910Python def strict_sum(*values): if len(values) == 0: print("Warning: No values received.") return 0 return sum(values) print(strict_sum()) # Output: Warning: No values received. -> 0
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 positional arguments named prices in the
calculate_totalfunction. - If no arguments are provided, return
"Your cart is empty.". - Discounts are mutually exclusive – apply only the highest applicable one:
- Apply a 20% discount if the total is $200 or more.
- Apply a 10% discount if the total is $100 or more.
- Apply no discount otherwise.
- Return the final total as a formatted string:
"Final total: $X.XX".
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat