W3docs

Use of *args and **kwargs

In Python, *args and **kwargs are special keywords that allow you to pass a variable number of arguments to a function.

In Python, *args and **kwargs are special keywords that allow you to pass a variable number of arguments to a function.

*args is used to pass a non-keyworded, variable-length argument list to a function. For example:

*Pass a non-keyworded, variable-length argument list to a function in Python using args

python— editable, runs on the server

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

**kwargs is used to pass a keyworded, variable-length argument list to a function. For example:

**Pass a keyworded, variable-length argument list to a function in Python using kwargs

python— editable, runs on the server

Both *args and **kwargs can be used to pass arguments to a function in a flexible way, allowing you to pass a varying number of arguments to a function.

You can also use both *args and **kwargs in the same function definition, but *args must come before **kwargs.

For example:

**Use *args and kwargs in one function in Python

python— editable, runs on the server

This would print:


1
2
3
name = Alice
age = 20