Sometimes a function needs to accept a variable number of arguments. Python provides *args and **kwargs for this purpose. They allow a function to receive more arguments than the number of parameters explicitly defined.
What is *args?
*args allows a function to accept any number of positional arguments.
The name args is a convention. You can use another name, but the * is what tells Python to collect multiple positional arguments.
def add(*args):
print(args)
add(10, 20, 30)Output:
(10, 20, 30)The arguments are collected into a tuple.
This makes it possible to perform operations on any number of values:
def add(*args):
total = 0
for number in args:
total += number
return total
print(add(10, 20))
print(add(10, 20, 30, 40))Output:
30
100You can also use *args when the function may receive zero or more positional arguments:
def show(*args):
for item in args:
print(item)
show("Python", "Django", "React")Output:
Python
Django
ReactUsing *args
Since args is a tuple, you can access its elements using indexing and iterate over it like any other tuple.
def numbers(*args):
print("First:", args[0])
print("Total:", len(args))
numbers(5, 10, 15, 20)Output:
First: 5
Total: 4*args is useful when the number of positional arguments is not known in advance.
For example:
def largest(*numbers):
return max(numbers)
print(largest(10, 25, 7, 40, 15))Output:
40What is **kwargs?
**kwargs allows a function to accept any number of keyword arguments.
The name kwargs is a convention, while ** tells Python to collect the keyword arguments.
def student(**kwargs):
print(kwargs)
student(name="Rahul", age=21, course="Python")Output:
{'name': 'Rahul', 'age': 21, 'course': 'Python'}Unlike *args, which collects values into a tuple, **kwargs collects keyword arguments into a dictionary.
Each parameter name becomes a dictionary key.
def student(**kwargs):
for key, value in kwargs.items():
print(key, ":", value)
student(name="Rahul", age=21, course="Python")Output:
name : Rahul
age : 21
course : PythonYou can pass any number of keyword arguments:
student(name="Rahul")
student(name="Rahul", age=21)
student(name="Rahul", age=21, course="Python")Using **kwargs
Since kwargs is a dictionary, you can access its values using keys and use dictionary methods.
def employee(**details):
print("Name:", details["name"])
print("Department:", details["department"])
employee(name="Amit", department="IT")Output:
Name: Amit
Department: ITYou can also safely access a value using get():
def employee(**details):
print(details.get("name", "Unknown"))
employee(name="Amit")Output:
AmitIf no name argument is supplied, "Unknown" will be used.
employee()Output:
UnknownUsing Both Together
A function can use both *args and **kwargs. In this case, *args collects positional arguments and **kwargs collects keyword arguments.
def display(*args, **kwargs):
print("Positional:", args)
print("Keyword:", kwargs)
display(10, 20, name="Rahul", age=21)Output:
Positional: (10, 20)
Keyword: {'name': 'Rahul', 'age': 21}Here:
- 10 and 20 are collected by *args.
- name="Rahul" and age=21 are collected by **kwargs.
Another example shows how both can be used inside a function:
def show_details(*args, **kwargs):
print("Subjects:", args)
for key, value in kwargs.items():
print(key, ":", value)
show_details("Python", "Django", name="Abhishek", age=22)Output:
Subjects: ('Python', 'Django')
name : Abhishek
age : 22When both are used in a function definition, the usual order is:
def function(*args, **kwargs):
pass*args handles extra positional arguments, while **kwargs handles extra keyword arguments. This is especially useful when designing flexible functions that can work with different numbers of inputs.