Arguments, *args, **kwargs
Published by
sanya sanya
A function in Python has the ability to accept arguments, or inputs supplied to the function when it is called. These arguments may be positional arguments, *args, or **kwargs, among other types.
# Positional Arguments
In Python, positional arguments are the most prevalent kind. They are required and are identified by their place in the function call. This implies that they cannot be skipped and must be supplied to the function in the proper order.
Consider the following function, which computes the sum of two numbers, as an illustration:

The positional arguments for this function are x and y. The x and y values must be supplied in the proper order when calling the function:

In this example, the values for x and y are passed as 2 and 3, respectively.
# *args
Python has a specific syntax called *args that enable functions to take any number of positional arguments. The syntax is args, where the '' indicates that a tuple of arguments should be created.
Take the following function, for instance, which determines the sum of any quantity of numbers:

This function adds the positional arguments it receives in any number. The arguments are condensed into a tuple and allocated to the args parameter when the function is called.

The positional parameters 2 and 3 are passed in the initial call and are contained in the args tuple. In the second call, the positional arguments 2, 3, 4, and 5 are supplied and contained in the args tuple.
# **kwargs
Another unique syntax in Python that enables a function to accept any number of keyword arguments is **kwargs. The syntax is kwargs, where the ‘’ stands for "should be packed into a dictionary," indicating that the arguments should be.
Take the following function, for instance, which outputs the key-value combinations for any number of keyword arguments:

This function prints out each key-value pair for any number of keyword arguments. The arguments are compiled into a dictionary and assigned to the 'kwargs' parameter when the function is invoked.

The function is called with the keyword arguments age=21 and name="Ankit" packed into the 'kwargs' object. The 'print_kwargs' function then loops through the object, printing each key-value pair.
# Using **kwargs and *args together
Additionally, combining ‘**kwargs’ and ‘*args’ into a single function is conceivable. In the function signature, ‘*args’ must occur before ‘**kwargs’. For instance:

'arg1' and 'arg2' are positional parameters in this example, whereas '*args' is used to accept any number of additional positional arguments, and '**kwargs' is used to take any number of additional keyword arguments.

In this call, 1 and 2 are passed as the positional arguments, and 3 and 4 are packed into the 'args' tuple. The keyword arguments name="Ankit" and age=21 are packed into the 'kwargs' object.
# Conclusion
In Python, there are three types of function arguments: positional arguments, *args, and **kwargs. Positional arguments are the most common type of argument and are defined by their position in the function call. *args and **kwargs are special syntaxes that allow a function to accept an arbitrary number of positional and keyword arguments, respectively. By combining *args and **kwargs in a single function, it is possible to create a function that can accept any combination of positional and keyword arguments.
Library
WEB DEVELOPMENT
FAANG QUESTIONS

