*Args and **Kwargs

What are *args and **kwargs?

Python’s *args and *kwargs are special parameters used in function definitions to allow for undefined number or arguments. *args captures any extra positional arguments passed into the function as a tuple while **kwargs captures any additional keyword arguments as a dictionary thus help in creating functions that can adapt to various inputs in short making functions reusable and versatile.

Why use *args and **kwargs?

By using *args and **kwargs functions can handle optional or unknown numbers of arguments which is useful in cases where the number of inputs is unpredictable. For example, when organizing an event in a barangay you might want to record a varying number of expenses. With *args and **kwargs the function can adjust to any number of input making it ideal for these dynamic cases.

Tutorials dojo strip

How to use it?

To use *args and *kwargs, simply define a function and add it as function arguments.

Syntax:

Example

A simple program to calculate the total expenses in a Filipino Family Gathering.

Tutorials dojo strip