*args and **kwargs in Python

Sometimes we don't know how many values will be passed to a function. In such cases, Python provides two special symbols:

  • *args → to accept many positional arguments
  • **kwargs → to accept many keyword (named) arguments

What is *args?

*args allows a function to take any number of values (without names). These values are stored as a tuple.

Example: Using *args

def total_marks(*args):
    sum = 0
    for mark in args:
        sum += mark
    print("Total Marks:", sum)

total_marks(70, 80, 90)

Output:

Total Marks: 240

Explanation:

  • *args collects (70, 80, 90) as a tuple.
  • The loop adds each value one by one.
  • Finally, the total is printed.

What is **kwargs?

**kwargs allows a function to take any number of named arguments. These arguments are stored in the form of a dictionary.

Example: Using **kwargs

def student_info(**kwargs):
    for key, value in kwargs.items():
        print(key + ":", value)

student_info(name="Vanshu", age=19, course="Python")

Output:

name: Vanshu
age: 19
course: Python

Explanation:

  • **kwargs collects all arguments as key-value pairs.
  • These are printed using a loop over the dictionary.

Using both *args and **kwargs in one function

def show_all(*args, **kwargs):
    print("Values:", args)
    print("Named Info:", kwargs)

show_all(10, 20, name="Simran", subject="Python")

Output:

Values: (10, 20)
Named Info: {'name': 'Simran', 'subject': 'Python'}

Explanation:

  • *args stores (10, 20) in a tuple.
  • **kwargs stores {'name': 'Simran', 'subject': 'Python'} as a dictionary.

Summary Table:

Term Used For Stored As
*args Multiple values (without names) Tuple
**kwargs Multiple values (with names) Dictionary

Key Points to Remember:

  • *args lets us pass many values without names.
  • **kwargs lets us pass many values with names.
  • They make our functions more flexible.
  • We can use both in the same function, but *args must come before **kwargs.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar