First, let’s start with a little background. In Python, you can define functions that accept arguments from a function call. These types of functions are called parameterized functions. While defining a parameterized function, you need to define the arguments that the function will accept as its parameter values. Let’s see this with the help of an example:

The script below defines a function product(num1, num2) which accepts two parameter values: num1 and num2. The function returns the product of the two values.

def product(num1, num2):
    result = num1 * num2
    return result

While calling the product(num1, num2) function, you need to pass arguments for the num1 and num2 parameters. You can pass two and only two arguments. If you pass one argument, like in the following example, you’ll see an error:

product(4)

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: product() missing 1 required positional argument: 'num2'

Similarly, if you pass more than 2 arguments to the product(num1, num2) function, you will see the following error:

product(4,5,6)

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: product() takes 2 positional arguments but 3 were given

It’s kind of like Goldilocks. The call to the product(num1, num2) function will only be successful if you pass exactly 2 arguments for the num1 and num2 parameters, as shown in the script below:

product(4,5)

Output:

20

What if you don’t know the exact number of parameter values while calling the function? In simple words, what if you want your product() function to return the product of a different number of variables in each function call. For example, if you pass 2 argument values, the product() function returns the product of 2 values; if you pass 3 argument values, the product() function returns the product of the 3 values, and so on. This is where the *args argument comes to play.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.


Using *args Argument

By creating a function with an argument called *args, you can pass as many variables as you want to your function when making your function call. The *args argument is actually a list that stores a variable number of arguments passed in your function call.

Let’s see this with the help of an example. The following script defines the product(*args) function again with *args as the only argument. Inside the product function, a for loop iterates through the items in the *args list and returns the product of all the items. It is important to notice that within the function, you can access the *args list without the asterisk (*).

def product(*args):
    result = 1
    for val in args:
        result = result * val

    return result

To call the product(*args) function with the *args argument, you need to pass a list of values in the function call to the product(*args) function. It’s worth mentioning that you need to add the asterisk(*) sign with the list name that you pass in the call statement so the function knows to process it as an *args argument.

The script below defines a vals list with three items and passes it to our new product(*args) function we just made.

vals = [4,5,6]
product(*vals)

In the output, you can see the product of the three items that you passed in the list.

Output:

120

Since the *args argument accepts a list with variable number of items, let’s now pass a list to the same function, but this time we’ll include 5 items:

vals = [4,5,6,2,2]
product(*vals)

Output:

480

All 5 arguments were multipled together in our function, without any errors being generated. In addition to passing a list to the product(*args) function, you can directly pass list items (separated by commas) to the product(*args) function, like we do here in our next example:

product(4,5,6)

Output:

120

You can also use the *args argument in combination with fixed positional arguments. For example, the following script defines the sum_product() function, which accepts two positional arguments at the first and second positions. The third argument is the *args argument. The function takes the sum of the two positional arguments and then multiplies the result with the product of all the items in the *args list.

def sum_product(num1, num2, *args):
    result = num1 + num2
    for val in args:
        result = result * val

    return result

By structuring your function like this, you’re still able to generate and handle errors if someone enters too few arguments.

In the script below, we’ll pass 6 and 4 as values for our positional arguments. After that, we’ll pass a list of integers (4, 5 and 2) to our *args argument. The function will perform the following calculation: 6 + 4 = 10 , and then 10 x 4 x 5 x 2 = 400.

vals = [4,5,2]
sum_product(6,4, * vals)

Output:

400

You can see that with the *args argument, you’re able to pass a variable number of arguments to a single function and process those arguments however you want in your function.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.


Using **kwargs Argument

The **kwargs argument is similar to the *args argument, as it also allows you to pass a variable number of arguments to a function. However, the **kwargs argument accepts a dictionary of key-value pairs instead of a simple list.

Here’s an example. The script below defines a function named items_kw(**kwargs) which accepts a dictionary with a variable number of key-value pairs. The function then prints the keys and values for each item passed.

def items_kw(**kwargs):

    for key, value in kwargs.items():
        print(key, " - ", value)

Now that our function is definied, we’ll create a dictionary with three items and pass it to our new items_kw(**kwargs) function.

items_dic = {"Apple":10, "Mango": 15, "Banana":18}
items_kw(**items_dic)

In the output, you can see the keys and values from the items we passed to our items_kw(**kwargs) fuction.

Output:

Apple  -  10
Mango  -  15
Banana  -  18

Similarly, you can directly pass keys and value pairs (separated by commas) in a function call to a function with **kwargs argument, just like this:

items_kw(Apple= 25, Mango=10, Banana= 35)

Output:

Apple  -  25
Mango  -  10
Banana  -  35

This lets you use and process and handle keyword arguments without requiring explicitly definitions in your function declaration.

Hopefully you found this tutorial helpful! For more Python tips, go ahead and subscribe using the form below.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.