Arbitrary 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.
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.
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.". - 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.17
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.
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.". - 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