args and kwargs
Accept variable-length positional and keyword arguments.
args and kwargs is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction
What is *args?
def f(*args):
print(args)
f(1, 2, 3) # (1, 2, 3)Iterating *args
def total(*args):
return sum(args)
print(total(1, 2, 3, 4, 5))What is **kwargs?
def f(**kwargs):
print(kwargs)
f(name='Alice', age=30)Using **kwargs
def display(**kwargs):
for k, v in kwargs.items():
print(f'{k}: {v}')
display(x=1, y=2)Combining Regular, *args, **kwargs
def f(a, *args, **kwargs):
print(a, args, kwargs)
f(1, 2, 3, x=4)Forwarding Arguments
def real(a, b, c=0): return a+b+c
def wrapper(*args, **kwargs): return real(*args, **kwargs)
print(wrapper(1, 2, c=3))Unpacking with * in Calls
def add(a, b, c): return a+b+c
args = [1, 2, 3]
print(add(*args))Extended Iterable Unpacking
first, *rest = [1, 2, 3, 4]
print(first, rest)
*head, last = [1, 2, 3]
print(head, last)Type Hints for *args/**kwargs
def f(*args: int, **kwargs: str) -> None:
print(args, kwargs)
f(1, 2, name='Alice')Common Patterns
lst = [1, 2, 3]
print(*lst) # 1 2 3Quick Check
Recap
Keep Going
Frequently asked questions
Is the “args and kwargs” lesson free?
Yes — the full text of “args and kwargs” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “args and kwargs”?
Accept variable-length positional and keyword arguments. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “args and kwargs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.