DEV Community

Cover image for Understanding *args and **kwargs in Python, Explained in Plain English
Brent Ochieng
Brent Ochieng

Posted on

Understanding *args and **kwargs in Python, Explained in Plain English

If you’ve ever written a Python function and wondered how to make it accept any number of inputs, you’re not alone. This is where two strangely named but incredibly powerful tools come in:

*args
**kwargs

Think of them as flexible containers that let your functions handle unpredictable or unlimited inputs — without breaking.

In this article, we’ll break them down using real-life examples, simple explanations, and copy-paste code.

1. What Is *args?

*args lets your function accept any number of positional arguments, even zero.

Think of it like this:

Imagine you’re at a supermarket checkout.
Some customers bring 2 items, some bring 20.
Instead of creating a different till for each customer, the cashier accepts any number of items.

***args **does exactly that for functions.

Example: Adding Any Number of Numbers

def add_numbers(*args):
    return sum(args)

print(add_numbers(2, 3))           # 5
print(add_numbers(10, 20, 30))     # 60
print(add_numbers())               # 0

Enter fullscreen mode Exit fullscreen mode

Whatever you pass into the function is packed into a tuple called args

2. What Is **kwargs?

****kwargs **lets your function accept any number of named arguments — things passed as key=value.

Real-life comparison:

Think of filling out a hotel registration form.

Every guest gives different details:

Name only
Name + email
Name + phone + nationality
The hotel needs one form flexible enough to accept anything.

***kwargs* works exactly like that.

Example: Guest Registration

def register_guest(**kwargs):
    return kwargs

print(register_guest(name="Brent", room=305))
# {'name': 'Brent', 'room': 305}

print(register_guest(name="Alice", email="alice@email.com", nights=3))
# {'name': 'Alice', 'email': 'alice@email.com', 'nights': 3}

Enter fullscreen mode Exit fullscreen mode

Everything goes into a dictionary called kwargs.

OUTPUT:

Toppings: ('cheese', 'beef')
Details: {'size': 'large', 'crust': 'thin'}

Enter fullscreen mode Exit fullscreen mode

3.Why *args and **kwargs Matter in Real Projects

Here’s why developers (and data analysts) rely on them:

✔ They make functions flexible
✔ They help you build reusable code
✔ They simplify APIs and utilities
✔ They clean up long function signatures
✔ They allow you to pass through unknown parameters

In short:
They help your code adapt instead of break.

One-sentence summary:

*args is a bag of unnamed values, and **kwargs is a dictionary of named values.

Final Thoughts

**args* and ***kwargs* are not advanced concepts but rather practical tools that make your functions smarter and more flexible.
Mastering them early will save you hours of debugging in the future.

Top comments (0)