Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 51

*args and **kwargs in Python

In this lesson: Pass variable arguments in Python with *args and **kwargs — syntax, unpacking and real examples with output. In Hindi & English.

Why args and kwargs

Sometimes you do not know in advance how many arguments a function will get. *args and **kwargs let a function accept any number of arguments. *args gathers extra positional values, and **kwargs gathers extra named values.

💡 *args → a tuple of extra positional values. **kwargs → a dict of extra named values.

*args — many positional values

def add(*args):
    total = 0
    for n in args:
        total += n
    return total

print(add(1, 2, 3))       # 6
print(add(5, 10, 15, 20)) # 50
6
50

The *args collects however many numbers you pass into a tuple, so the same function works for 3 numbers or 4 — or any count.

**kwargs — many keyword values

def info(**kwargs):
    for key, value in kwargs.items():
        print(key, "=", value)

info(name="Aman", age=18, city="Delhi")
name = Aman
age = 18
city = Delhi

**kwargs collects named arguments into a dictionary. You can loop its .items() to read each key and value.

Using both together

def demo(a, *args, **kwargs):
    print("a:", a)
    print("args:", args)
    print("kwargs:", kwargs)

demo(1, 2, 3, x=10, y=20)
a: 1
args: (2, 3)
kwargs: {'x': 10, 'y': 20}

The regular parameter a takes the first value, *args catches the rest of the positional ones, and **kwargs catches the named ones. Order matters: normal, then *args, then **kwargs.

Unpacking with * and **

The same symbols can spread a list or dict back into arguments when calling.

def add(a, b, c):
    return a + b + c

nums = [1, 2, 3]
print(add(*nums))               # spreads list -> add(1,2,3)

data = {"a": 1, "b": 2, "c": 3}
print(add(**data))              # spreads dict -> add(a=1,b=2,c=3)
6
6

Here * and ** do the reverse of collecting — they unpack a list into positional arguments and a dict into keyword arguments.

Common mistakes

  • Wrong parameter order. It must be normal params, then *args, then **kwargs.
  • Thinking the names are required. Only * and ** matter; the names are convention.
  • Mixing up the types. args is a tuple, kwargs is a dict — access them accordingly.
  • Forgetting to unpack. Passing a whole list where separate values are needed — use *list.

Summary

  • *args lets a function take any number of positional values (as a tuple).
  • **kwargs lets it take any number of named values (as a dict).
  • Order: normal parameters, then *args, then **kwargs.
  • Only * and ** matter — the names are convention.
  • Use * and ** when calling to unpack a list or dict.

Next, review the basics in Python functions, or explore list comprehension.

Frequently Asked Questions

What are *args and **kwargs in Python?
They let a function accept any number of arguments. *args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dictionary. They make functions flexible.
What is the difference between *args and **kwargs?
*args is for positional values passed without names (like add(1, 2, 3)). **kwargs is for named values (like info(name="Aman", age=18)). One gives a tuple, the other a dictionary.
Do the names have to be args and kwargs?
No. Only the * and ** matter. *args and **kwargs are just the common convention; *numbers or **options work exactly the same.
What order do parameters go in?
Regular parameters first, then *args, then keyword-only parameters, then **kwargs. Putting them in the wrong order causes a syntax error.
What does unpacking with * and ** do?
When calling a function, * spreads a list/tuple into positional arguments and ** spreads a dictionary into keyword arguments. It is the reverse of collecting them.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicMagic Methods (Dunder) Your progress: 51 / 66 (77%) · Lesson 52 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free