Combination of normal parameters and *args in Python
You can combine regular parameters and *args
in a function. However, in this case, *args
should always come last. Let's add two additional numeric parameters to our function and print them to the console along with *args
:
def func(num1, num2, *args):
print(num1, num2, args)
func(1, 2, 3, 4, 5) # 1 2 (3, 4, 5)
After executing the code, the numbers 1
and 2
will be output to the console separately from the tuple of other numbers, since the system assigned them to num1
and num2
, respectively:
1 2 (3, 4, 5)
What will be the result of running the following code:
def func(num1, num2, *args):
return sum(args) + (num1 * num2)
print(func(10, 5, 1, 2, 3))