   Python Function Arguments: Using *args and **kwargs

When you write a function in Python, you can define the number of arguments it takes. However, sometimes you may want to create a function that can take a variable number of arguments. This is where *args and **kwargs come in.

## Using *args to pass a variable number of non-keyword arguments

*args is used to pass a variable number of non-keyword arguments to a function. Non-keyword arguments are positional arguments, meaning they don't have a keyword associated with them.

Here's an example:

``````def calculate_sum(*args):
sum = 0
for arg in args:
sum += arg
return sum

result = calculate_sum(1, 2, 3)
print(result) # Output: 6``````

In the example above, we defined a function called `calculate_sum` that takes a variable number of arguments using *args. We then looped through all the arguments passed to the function and added them up to get the sum.

We called the function with three arguments (1, 2, and 3), and it returned the sum of those numbers.

## Using **kwargs to pass a variable number of keyword arguments

**kwargs is used to pass a variable number of keyword arguments to a function. Keyword arguments are named arguments, meaning they have a keyword associated with them.

Here's an example:

``````def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

print_info(name="John", age=30, country="USA")``````

In the example above, we defined a function called `print_info` that takes a variable number of keyword arguments using **kwargs. We then looped through all the arguments passed to the function and printed them.

We called the function with three keyword arguments (name, age, and country), and it printed out their values.

## Combining *args and **kwargs in a function definition

You can also combine *args and **kwargs in a function definition to create a function that can handle both non-keyword and keyword arguments.

Here's an example:

``````def print_values(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f"{key}: {value}")

print_values(1, 2, name="John", age=30)``````

In the example above, we defined a function called `print_values` that takes a variable number of both non-keyword and keyword arguments. We looped through all the non-keyword arguments passed to the function and printed them, and then looped through all the keyword arguments passed to the function and printed them.

We called the function with two non-keyword arguments (1 and 2) and two keyword arguments (name and age), and it printed out all four values.

## Exercises

1. Write a function that takes a list of numbers as its argument and returns the sum of those numbers.
2. Write a function that takes any number of strings as its argument and concatenates them together, separating each string with a space.
3. Write a function that takes a dictionary as its argument and prints out its key-value pairs.

In conclusion, *args and **kwargs are powerful tools that allow you to create flexible functions that can handle different types of input. Whether you're working with positional or keyword arguments, or both, these tools make it easy to create functions that can handle any kind of input. Top Tutorials