In Python, arbitrary arguments (also called variable-length arguments) allow a function to accept any number of inputs.
This is very useful when you don’t know in advance how many values will be passed into a function.
Python provides two types:
-
*args→ Variable number of positional arguments -
**kwargs→ Variable number of keyword arguments
🔹 What are Arbitrary Arguments?
Arbitrary arguments let a function accept:
✔ Unlimited values
✔ Flexible inputs
✔ Dynamic function calls
Instead of fixed parameters, Python collects them into:
-
A tuple (
*args) -
A dictionary (
**kwargs)
🔹 1. *args (Arbitrary Positional Arguments)
The *args syntax allows you to pass multiple positional arguments.
🔹 Syntax of *args
def function_name(*args):
pass
🔹 Example of *args
def total(*args):
print(args)
total(10, 20, 30, 40)
🔸 Output:
(10, 20, 30, 40)
🔹 Example: Sum of Numbers
def add(*numbers):
result = sum(numbers)
print("Sum:", result)
add(5, 10, 15)
add(1, 2, 3, 4, 5)
🔸 Output:
Sum: 30
Sum: 15
🔹 Loop through *args
def show_items(*items):
for item in items:
print(item)
show_items("Apple", "Banana", "Mango")
🔸 Output:
Apple
Banana
Mango
🔍 Key Point:
-
*argsis stored as a tuple
🔹 2. **kwargs (Arbitrary Keyword Arguments)
The **kwargs syntax allows a function to accept any number of keyword arguments.
🔹 Syntax of **kwargs
def function_name(**kwargs):
pass
🔹 Example of **kwargs
def info(**kwargs):
print(kwargs)
info(name="John", age=25, city="Phnom Penh")
🔸 Output:
{'name': 'John', 'age': 25, 'city': 'Phnom Penh'}
🔹 Loop through **kwargs
def display(**data):
for key, value in data.items():
print(key, ":", value)
display(name="Anna", job="Developer", country="USA")
🔸 Output:
name : Anna
job : Developer
country : USA
🔍 Key Point:
-
**kwargsis stored as a dictionary
🔹 Real-Life Example: Student System (*args)
def marks(*scores):
print("Total:", sum(scores))
marks(80, 90, 85)
🔸 Output:
Total: 255
🔹 Real-Life Example: User Profile (**kwargs)
def profile(**user):
print("User Profile:")
for k, v in user.items():
print(k, ":", v)
profile(name="Sophy", age=22, country="Cambodia")
🔹 Mixing Normal, *args, and **kwargs
Python allows combining all types:
def demo(a, b, *args, **kwargs):
print("a:", a)
print("b:", b)
print("args:", args)
print("kwargs:", kwargs)
demo(1, 2, 3, 4, 5, name="Alex", age=30)
🔸 Output:
a: 1
b: 2
args: (3, 4, 5)
kwargs: {'name': 'Alex', 'age': 30}
🔹 Rule of Order ⚠️
When combining arguments, the order must be:
1. Normal arguments
2. *args
3. **kwargs
🔹 ❌ Wrong Order Example
def test(**kwargs, *args):
pass
❌ This will cause an error
🔹 Why Use *args and **kwargs?
✔ Makes functions flexible
✔ Handles unknown number of inputs
✔ Useful for APIs and libraries
✔ Reduces need for multiple function versions
✔ Improves code scalability
🔹 Difference Between *args and **kwargs
| Feature | *args | **kwargs |
|---|---|---|
| Type | Positional arguments | Keyword arguments |
| Stored as | Tuple | Dictionary |
| Syntax | *args | **kwargs |
| Usage | Multiple values | Named values |
🚀 Conclusion
Python arbitrary arguments (*args and **kwargs) make functions extremely powerful and flexible. They allow you to handle dynamic inputs easily without limiting the number of arguments.
They are widely used in:
- APIs
- Frameworks
- Libraries
- Real-world applications


0 Comments