
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 multiply 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: Function with fixed parameters
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.
In the following example, we define a simple function that takes two arguments and adds them together. The number of arguments is fixed to two -
def add(num1, num2): return num1+num2 print(add(4,5))
The output generated is as follows -
9
Example: Function with variable-length arguments
In this example, we modify the function to accept a variable number of arguments using *args. This allows the function to sum any number of arguments passed to it -
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) result = add_num(5, 6, 7, 8, 9) print('Sum is', result)
The output generated is as follows -
Sum is 6 Sum is 100 Sum is 35
Example: Multiply function with variable-length arguments
In the example below, we define a multiply function that accepts a variable number of arguments and multiplies them together. It shows how variable-length arguments can be used in a function to handle multiple inputs dynamically -
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)
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: Function with **kwargs
In this example, we define a function that accepts keyword arguments using **kwargs. The function then prints the data type of the argument and iterates through the dictionary, printing each key-value pair passed into the function -
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)
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