Lazy Programming Series

Lazy Programming Series – *ARGS AND **KWARGS in Python

*args and **kwargs are special syntax in Python used for function definitions, allowing you to pass a variable number of arguments and keyword arguments, respectively, to a function.

*args:

*args is used to pass a variable number of positional arguments to a function. It collects all the positional arguments passed to the function into a tuple.

Example:

Output:

**kwargs:

**kwargs is used to pass a variable number of keyword arguments to a function. It collects all the keyword arguments passed to the function into a dictionary.

Example:

Output:

In this example, **kwargs collects the keyword arguments name="Alice" and age=30 into a dictionary named kwargs.

Combined usage:

You can also use *args and **kwargs together in a function definition to accept both positional and keyword arguments.

Example:

Output:

In this example, the function my_function accepts both positional arguments (1, 2, 3) and keyword arguments name="Alice" and age=30.

@SAKSHAM DIXIT

Hi, I’m saksham dixit

Leave a Reply

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