what are args and **kwargs in Python

In Python, *args and **kwargs are special syntaxes used to pass a variable number of arguments to a function.*args allows a function to accept a variable number of positional arguments, which are stored in a tuple called args.*(single asterisk) and **(double asterisk) are special symbols used in python to pass a variable no. of arguments to a function.

  • Argument passing: * is used to pass a variable number of positional arguments to a function, e.g., def my_function(*args):.
  • Keyword argument passing: ** is used to pass a variable number of keyword arguments to a function ,e.g.,def my_function(**kwargs):.

*args in python

In Python, *args (single asterisk) is a special syntax used in function definitions to pass a variable number of positional arguments to a function. The *args parameter collects any number of positional arguments passed to the function and packs them into a tuple. This syntax is particularly useful when you want to define a function that can accept an arbitrary number of arguments without specifying them explicitly in the function definition.

Example:
def sum_of_values(*args):
    total=0  #initially ,total is zero
    for number in args:
        total+=number
    return total
# Calling the function with different no. of arguments
print(sum_of_values(1,2,3))
print(sum_of_values(5,10,15,20))
print(sum_of_values(2,4,6,8,10,12))

output:
6
50
42

Uses of *args:

*args is commonly used in situations where you need to :

  1. Handle a variable no. of args.
  2. pass a list or tuple of args to a function.
  3. create a function that can take optional args.

**kwargs in python

**kwargs (double asterisks) in Python is a special syntax that allows a function to accept a variable number of keyword arguments. When you call the function, you can pass as many keyword arguments as you need, using the syntax keyword=value. All the keyword arguments you pass are stored in a dictionary called kwargs. These can be used to access inside the function. This dictionary holds all the keyword arguments you provided, with the keywords as keys and the values as their associated values. To use these arguments, you can access them by their keyword names, similar to how you would access items in a dictionary (e.g., kwargs[‘keyword’]).

def my_function(**kwargs):
    print(kwargs)

my_function(name='Speedy',age=30,city='Hyderabad')

Output:

{‘name’: ‘Speedy’, ‘age’: 30, ‘city’: ‘Hyderabad’}

Uses of **kwargs:

**kwargs is commonly used in situations where you need to:

  1. Handle a variable no. of keyword args
  2. Pass a dictionary of args to a function
  3. Create a function that can take optional keyword args

To summarize, *args and **kwargs are powerful features in Python that gives the flexibility and versatility of functions.Together, *args and **kwargs empower developers to write more adaptable, customizable, and concise code, contributing to the expressive and versatile nature of Python programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top