Python *args

Python *args parameter in a function definition allows the function to accept multiple arguments without knowing how many arguments. In other words it lets the function accept a variable number of arguments.

Datatype of args is tuple. Inside the function, you can access all the arguments passed as *args using a for loop. Or, you can use index to access the individual arguments. We will verify the datatype of args in an example below.

In this tutorial, we will learn how to use *args in function definition with the help of example programs.

Example: Python *args

We already know that *args parameter lets the function accept any number of arguments. In this example, we will write an addition function that can accept any number of arguments and returns the addition of all these arguments.

Python Program

def addition(*args):
    result = 0
    for arg in args:
        result += arg
    return result

if __name__ == "__main__":
    sum = addition(2, 5, 1, 9)
    print(sum)

    sum = addition(5)
    print(sum)

    sum = addition(5, 4, 0.5, 1.5, 9, 2)
    print(sum)
Run this program

Output

17
5
22.0

args in *args is just a name

args is just the parameter name. You can provide any name, instead of args, just like any other parameter in function definition. Asterisk symbol (*) before the parameter name is the important part. It tells Python that this parameter is going to accept a variable number of arguments. Because of its functionality, the asterisk symbol is called unpacking operator.

We shall use the same example above, and use a different name for args, say numbers.

Python Program

def addition(*numbers):
    result = 0
    for number in numbers:
        result += number
    return result

if __name__ == "__main__":
    sum = addition(2, 5, 1, 9)
    print(sum)
Run this program

Output

17

args in *args is a tuple

Based on the previous examples, it is already established that we can use args as an iterator. Well! if we could use an iterator on args, then what could be the datatype of args. Only one way to find out. Use Python type() builtin function.

Python Program

def addition(*numbers):
    print(type(numbers))

if __name__ == "__main__":
    addition(2, 5, 1, 9)
Run this program

Output

<class 'tuple'>

Loud and clear as it says. The datatype of args parameter is tuple. So, we get a tuple when we use unpacking operator with parameter (*args) to accept variable number of arguments.

Since tuple is immutable (at least shallow level), it is logical to keep the datatype of args as tuple instead of list.

*args with other parameters

*args is just another parameter that can accept multiple number of positional arguments. You can use *args with other parameters in your function definition.

In the following example, we will create a function that will accept arguments for some specified parameters, and then any number of arguments using *args.

Python Program

def calculator(operation, *numbers):
    if operation == "add":
        result = 0
        for num in numbers:
            result += num
        return result
    
    if operation == "product":
        result = 1
        for num in numbers:
            result *= num
        return result

if __name__ == "__main__":
    x = calculator("add", 2, 5, 1, 9)
    print(x)
    x = calculator("product", 3, 5, 2)
    print(x)
Run this program

Output

17
30

*args with **kwargs

While *args can accept any number of positional arguments, Python **kwargs can accept any number of named arguments.

You can use *args and **kwargs in a function definition to accept both positional arguments and named arguments, whose count is unknown.

In the following example, we will define a function with both *args and **kwargs.

Python Program

def myFunction(*args, **kwargs):
    print(args)
    print(kwargs)

if __name__ == "__main__":
    myFunction("hello", "mars", a = 24, b = 87, c = 3, d = 46)
Run this program

Output

('hello', 'mars')
{'a': 24, 'b': 87, 'c': 3, 'd': 46}

Just to remind, the datatype of args is tuple, and the datatype of kwargs is dictionary.

Summary

In this tutorial of Python Examples, we learned how to use *args to write functions that can accept any number of arguments.