What does *args mean in Python?

Experience Level: Junior
Tags: Python

Answer

The asterisk before a method or function parameter name means that multiple arguments can be passed to the method/function.

Most frequently, *args is used, however, the name doesn't necessarily be only *args, but it can be *anythingElse.

Example:

def add(*numbers)
    total = 0
    for num in numbers:
        total += num
    return total

print(add(1, 5, 2)) # Displays 8

print(add(1)) # Displays 1

print(add(5, 2)) # Displays 7

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself