Non keyword arguments (args)
*args is known as Non keyword arguments which is used to pass the unknown number of arguments or variable number of arguments to the function.
In the function, Non keyword arguments are denoted using asterisk symbol before the parameter name like as *args, *numbers etc.
The arguments are passed like function (argl, arg2, ..., arg3) and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk *.
Example 1:
def func(*args):
print('Non keyword arguments:', args)
for arg in args:
print(arg, end=' ')
func(1, 2, 3, 4)
Output
Non keyword arguments: (1, 2, 3, 4)
print1234
Example 2:
def func(sum, *numbers):
print('sum =', sum)
print('Numbers:', numbers)
for n in numbers:
sum += n
print('Sum of numbers is:', sum)
func(0, 1, 2, 3, 4)
Output
sum = 0
Numbers: (1, 2, 3, 4)
Sum of numbers is: 10
Top comments (0)