Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Default, Keyword, *args & **kwargs

15 min 30 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 2 of 6 · View course roadmap
Step 1

Learn the idea

Python's argument system is wonderfully flexible:

  • Defaults: def greet(name, punct="!") — callers may omit punct
  • Keyword args: greet(punct="?", name="Bo") — order-free, self-documenting
  • *args: collects extra positional args into a tuple — def total(*nums)
  • **kwargs: collects extra keyword args into a dict

Classic trap: never use a mutable default like def f(items=[]) — the list is created once and shared between calls. Use items=None and create inside.

Where you'll use this

Open any library's docs: requests.get(url, params=None, timeout=None, **kwargs) — defaults, keywords and kwargs everywhere. Understanding this lesson is understanding how every Python API is designed.

Common mistakes

  • The mutable default trap: def add(item, items=[]) shares ONE list across all calls. It's the most famous Python gotcha — use items=None then items = items or [].
  • Positional after keyword: f(x=1, 2) is a SyntaxError — keywords go last.
  • Forgetting that *args is a tuple inside the function — you can loop it and len() it.

Pro tip

The * also unpacks at call time: print(*[1, 2, 3]) is print(1, 2, 3), and merge = {**d1, **d2} for dicts. Packing and unpacking are the same symbol in opposite directions.

Step 2

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 3

Pass the challenge +30 XP

Blank · autosaved

Write describe(name, role="student") that returns "{name} is a {role}". Print describe("Ada") and describe("Grace", role="admiral").

Target output
Ada is a student
Grace is a admiral
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. What does *args collect?
2. Why is def f(x, items=[]) dangerous?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path