Back
+10 XP
Concept

Arbitrary Arguments: `*args`

Prefix a parameter with `*args` to collect any number of positional arguments into a single tuple inside the function.

python
def total_loot(*coins):
    print("Received tuple:", coins)
    return sum(coins)

print(total_loot(10, 20, 50))  # 80

# 94. Arbitrary Arguments: `*args` Prefix a parameter with `*args` to collect any number of positional arguments into a single tuple inside the function: ```python def total_loot(*coins): print("Received tuple:", coins) return sum(coins) print(total_loot(10, 20, 50)) # 80 ``` The name `args` is an industry convention—the magic is strictly the asterisk `*`. You can pass zero, one, or a hundred arguments, and Python packages them into an immutable tuple.

Checkpoint

What data type is `args` inside a function defined with `*args`?

Practice

Write a function `multiply_all(*nums)` that multiplies all passed numbers together and returns the product. Call `multiply_all(2, 3, 4)` and print the result.

Terminal
01
Tap code blocks below to assemble
Available Blocks