How to use variable-length arguments in a function in Python?


As the name implies, an argument with a variable length can take on a variety of values. You define a variable argument using a '*', for example *args, to show that the function can take a variable number of arguments.

Observations on Python's variable-length arguments are as follows.

  • The designation "*args" for variable length arguments is not required. The only thing needed is *; the variable name can be anything, like *names or *numbers.

  • You can send zero or more arguments to a function using a variable length argument.

  • A tuple is used to store the values passed to *args.

  • A formal argument may come before a variable args but not after one. You can use keyword arguments following a variable argument.

*args in function

To pass a variable number of arguments to a function in Python, use the special syntax *args in the function specification. It is used to pass a variable-length, keyword-free argument list. By convention, the sign * is frequently used with the word args in the syntax for taking in a variable number of arguments.

You can accept additional arguments using *args than the number of formal arguments you previously defined. Any number of additional arguments can be added to your current formal parameters using *args (including zero extra arguments).

For instance, we wish to create a multiply function that can multiple any number of inputs simultaneously. The use of variable parameters makes your function more adaptable in situations where the precise number of arguments is unknown at first. Imagine that you have a function that adds numbers.

Example 1

The following example demonstrates the usage of a regular function with fixed number of parameters.

def add(num1, num2): return num1+num2 print(add(4,5))

Output

The output generated is as follows.

9

You can specify that a function accepts a variable number of arguments and can be used to add up to 'n' numbers by altering the argument to *args.

Example 2

The following example demonstrates the usage of a function that takes variable length arguments.

def add_num(*args): sum = 0 for num in args: sum += num return sum result = add_num(1, 2, 3) print('Sum is', result) result = add_num(10, 20, 30, 40) print('Sum is', result) 22 result = add_num(5, 6, 7, 8, 9) print('Sum is', result)

Output

The output generated is as follows.

Sum is 6
Sum is 100
Sum is 35

Example 3

Let us see another example for this −

def multiply(*args): y = 1 for num in args: y *= num print(y) multiply(3, 7) multiply(9, 8) multiply(3, 4, 7) multiply(5, 6, 10, 8)

Output

The output generated is as follows.

21
72
84
2400

**kwargs in a function

A keyworded, variable-length argument list is passed using the specific syntax **kwargs in Python function declarations. With the double star, we use the name kwargs. The double star's ability to pass through keyword arguments is the cause for this (and any number of them).When passing a variable into a function, you can give it a name using a keyword parameter.

The kwargs can be viewed as a dictionary that associates each term with the value that is passed along with it. Because of this, there doesn't appear to be any sequence in which the kwargs were printed out when we iterate through them.

Example

The following example takes arguments using **kwargs.

def intro(**data): print("\nData type of argument:",type(data)) for key, value in data.items(): print("{} is {}".format(key,value)) intro(EmployeeName="George", Lastname="Jackson", Age=22, Phone=1234567890) intro(Firstname="James", Lastname="Jude", Email="jamesjude@nomail.com", Country="USA", Age=25, Phone=9876543210)

Output

The output generated is as follows.

Data type of argument: <class 'dict'>
EmployeeName is George
Lastname is Jackson
Age is 22
Phone is 1234567890

Data type of argument: <class 'dict'>
Firstname is James
Lastname is Jude
Email is jamesjude@nomail.com
Country is USA
Age is 25
Phone is 9876543210

Updated on: 16-Sep-2022

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements