📘 Lesson  ·  Lesson 83

*args and **kwargs

Flexible Arguments

💡 At a Glance

*args lets a function take any number of positional arguments; **kwargs takes any number of keyword arguments.

*args (many values)

Python
def total(*args):
    return sum(args)

print(total(1, 2, 3))       # 6
print(total(10, 20, 30, 40)) # 100
6 100

**kwargs (named values)

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

show(name="Aman", marks=88)
name = Aman marks = 88

Summary

  • *args collects extra positional args into a tuple.
  • **kwargs collects extra keyword args into a dictionary.
← 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.