Args Unpacking in Python

Level 392 keystrokes

This function accepts any number of arguments using *args. The asterisk collects all positional arguments into a tuple.

Python code

# *nums collects all arguments into a tuple called nums
1def add_all(*nums):
# sum() adds all numbers in the tuple
2 return sum(nums)
3
# Call with 3 arguments - nums becomes (1, 2, 3)
4print(add_all(1, 2, 3))
# Call with 4 arguments - nums becomes (10, 20, 30, 40)
5print(add_all(10, 20, 30, 40))

Output

6
100
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • (× 6
  • )× 6
  • ,× 5
  • _× 3
  • :× 1
  • *× 1

More Python examples at level 3

← Python typing practice