Star Args in Python

Level 383 keystrokes

This defines a function with *args to accept any number of positional arguments as a tuple.

Python code

# *args collects any number of positional arguments into a tuple
1def sum_all(*args):
# sum() adds up every value in the tuple
2 return sum(args)
3
# Inside the function, args is (1, 2, 3, 4, 5)
4result = sum_all(1, 2, 3, 4, 5)
# Prints 15
5print(result)

Output

15
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.

  • ,× 4
  • (× 4
  • )× 4
  • _× 2
  • :× 1
  • *× 1
  • =× 1

More Python examples at level 3

← Python typing practice