Skip to content
dreamcode
dreamcode
Map
*args & **kwargs
Lesson 37 of 77
+15 XP on finish
PYTHON INTERMEDIATEChapter 6 · Comprehensions and data tools

Flexible arguments

Put * before a parameter and it collects any extra positional arguments into a tuple. Put ** before one and it collects extra keyword arguments into a dict. The names args and kwargs are just a convention. The stars work the other way when calling: total(*values) spreads a list into separate arguments.

Worked example

How it reads

  • *numbers is a tuple of every positional argument, even none
  • **details is a dict of every name=value argument
  • total(*scores) unpacks the list into three separate arguments
Cloud tip: Order matters in a definition: normal parameters, then *args, then keyword-only ones, then **kwargs.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Change shout to take any number of words with *words and return them in capitals, joined by spaces, so shout("clear", "skies") prints CLEAR SKIES!.

Press Run to check your work.