🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Arguments (*args)

AI & DATA SCIENCE // arguments-args

*args collects any number of extra positional arguments passed to a function into a tuple, letting the function accept a variable number of inputs.

Syntax

def func(*args):
    for arg in args:
        ...

func(1, 2, 3)

Deep Dive Course

When a parameter is prefixed with a single asterisk, Python gathers every remaining positional argument into a tuple bound to that name — the name args is just a convention, not a requirement. This is how built-ins like print() and max() accept any number of arguments. The same asterisk can also be used at a call site to unpack an existing list or tuple into separate positional arguments.

1Understanding Arguments (*args)

When a parameter is prefixed with a single asterisk, Python gathers every remaining positional argument into a tuple bound to that name — the name args is just a convention, not a requirement. This is how built-ins like print() and max() accept any number of arguments. The same asterisk can also be used at a call site to unpack an existing list or tuple into separate positional arguments.

💡

Use a single asterisk at a call site to spread an existing list or tuple out into individual positional arguments, the mirror image of collecting them with *args in a function definition.

editor.html
def total(*args):
    return sum(args)

print(total(1, 2, 3, 4))
localhost:3000

2Practical Example

Here is a real-world application of **Arguments (*args)** showing how it is used in production Python code.

editor.html
def log(level, *messages):
    for msg in messages:
        print(f"[{level}] {msg}")

log("INFO", "Started", "Connected", "Ready")
localhost:3000

3Best Practices

Follow these guidelines when working with **Arguments (*args)**:

1. Use *args when a function genuinely needs to accept an unknown, variable number of positional inputs

2. Prefer explicit, named parameters over *args when the function only ever expects a fixed, known set of arguments

3. Combine *args with regular named parameters, which must come before it, when some arguments are required and the rest are open-ended

⚠️

Tip: Use a single asterisk at a call site to spread an existing list or tuple out into individual positional arguments, the mirror image of collecting them with *args in a function definition.

editor.html
def total(*args):
    return sum(args)

print(total(1, 2, 3, 4))
localhost:3000

Examples

Example 01Basic Usage
def total(*args):
    return sum(args)

print(total(1, 2, 3, 4))
Example 02Advanced Example
def log(level, *messages):
    for msg in messages:
        print(f"[{level}] {msg}")

log("INFO", "Started", "Connected", "Ready")

Best Practices

  • Use *args when a function genuinely needs to accept an unknown, variable number of positional inputs
  • Prefer explicit, named parameters over *args when the function only ever expects a fixed, known set of arguments
  • Combine *args with regular named parameters, which must come before it, when some arguments are required and the rest are open-ended

Interview Question

What's the difference between using *args in a function definition and using the same asterisk to unpack a list at a call site?

Hint: Same symbol, two directions.

In a function definition, *args collects an arbitrary number of positional arguments the caller passes in, gathering them into a tuple. At a call site, writing an asterisk before an existing list or tuple does the opposite: it unpacks that collection back out into separate positional arguments for the call, as if each element had been typed individually. The asterisk means 'gather' on the receiving side and 'spread' on the sending side.

Exercises

MediumPractice using Arguments (*args) in a real scenario.
View Solution
def total(*args):
    return sum(args)

print(total(1, 2, 3, 4))

Frequently Asked Questions

What's the difference between using *args in a function definition and using the same asterisk to unpack a list at a call site?

In a function definition, *args collects an arbitrary number of positional arguments the caller passes in, gathering them into a tuple. At a call site, writing an asterisk before an existing list or tuple does the opposite: it unpacks that collection back out into separate positional arguments for the call, as if each element had been typed individually. The asterisk means 'gather' on the receiving side and 'spread' on the sending side.

Related Functions

keyword-arguments-kwargsdef-keywordtuples