← Python Intermediate
7 / 25
Lesson 7 — args & kwargs
Flexible parameters
Sometimes a function should accept any number of arguments. *args collects extra positional arguments into a tuple, and **kwargs collects named ones into a dict. The names args and kwargs are just a convention; the stars do the work.
def show(*args, **kwargs):
print(args, kwargs)
show(1, 2, color="red") # (1, 2) {'color': 'red'}
Why it matters: print(), logging helpers and many library functions use this to stay flexible.
Want it another way?
Task
Make total() return the sum of however many numbers it is given.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.