In Python, *args and **kwargs are used to pass a variable number of arguments to a function.
They make your functions flexible and capable of handling different numbers of inputs dynamically.
🔹 1. *args → Non-Keyword Variable Arguments
- The
*argsparameter allows a function to accept any number of positional (non-keyword) arguments. - The function receives them as a tuple.
- The name
argsis just a convention — you can use any name (e.g.,*numbers).
🧩 Example 1: Using *args
def add_numbers(*args):
print("Arguments received:", args)
print("Sum:", sum(args))
add_numbers(10, 20)
add_numbers(5, 10, 15, 20)Output:
Arguments received: (10, 20)
Sum: 30
Arguments received: (5, 10, 15, 20)
Sum: 50Explanation:
All positional arguments are packed into a tuple (10, 20) or (5, 10, 15, 20).
🧩 Example 2: Iterating Over *args
def greet_people(*args):
for name in args:
print("Hello,", name)
greet_people("Dheeraj", "Ravi", "Amit")Output:
Hello, Dheeraj
Hello, Ravi
Hello, Amit🔹 2. **kwargs → Keyword Variable Arguments
- The
**kwargsparameter allows a function to accept any number of keyword arguments. - The function receives them as a dictionary where keys are argument names and values are argument values.
- The name
kwargsstands for keyword arguments (you can rename it, e.g.,**data).
🧩 Example 3: Using **kwargs
def print_info(**kwargs):
print("Keyword Arguments:", kwargs)
for key, value in kwargs.items():
print(f"{key} → {value}")
print_info(name="Dheeraj", age=24, city="Delhi")Output:
Keyword Arguments: {'name': 'Dheeraj', 'age': 24, 'city': 'Delhi'}
name → Dheeraj
age → 24
city → DelhiExplanation:
All keyword arguments are packed into a dictionary {key: value}.
🔹 3. Using Both *args and **kwargs Together
You can use both in the same function, but the order must be:
👉 def func(required, *args, **kwargs)
🧩 Example 4: Combining *args and **kwargs
def display_data(title, *args, **kwargs):
print("Title:", title)
print("Positional args:", args)
print("Keyword args:", kwargs)
display_data("Employee Details", "Dheeraj", 24, role="Developer", location="Delhi")Output:
Title: Employee Details
Positional args: ('Dheeraj', 24)
Keyword args: {'role': 'Developer', 'location': 'Delhi'}🧩 Example 5: Passing Lists and Dictionaries with * and **
You can unpack lists and dictionaries when calling a function.
def student_details(name, age, city):
print(f"Name: {name}, Age: {age}, City: {city}")
data_list = ["Dheeraj", 24, "Delhi"]
data_dict = {"name": "Ravi", "age": 22, "city": "Mumbai"}
student_details(*data_list) # Unpacks list
student_details(**data_dict) # Unpacks dictionaryOutput:
Name: Dheeraj, Age: 24, City: Delhi
Name: Ravi, Age: 22, City: Mumbai🔹 4. Order of Parameters in Function Definition
When using all argument types together, they should follow this order:
def func(normal, *args, default="Yes", **kwargs):
passOrder:
- Normal positional arguments
*args- Default arguments
**kwargs
🔹 In Summary
| Parameter | Type | Stored As | Used For |
|---|---|---|---|
*args | Variable-length positional arguments | Tuple | When you don’t know how many positional arguments you’ll get. |
**kwargs | Variable-length keyword arguments | Dictionary | When you don’t know how many keyword arguments you’ll get. |
✅ In short:
*args→ Used to pass multiple positional arguments (packed as a tuple).**kwargs→ Used to pass multiple keyword arguments (packed as a dictionary).- They make your functions flexible, dynamic, and more reusable.
