TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 16 of 22 · By Ethan Cole · Updated November 21, 2026

Python Function Arguments: Defaults, *args and **kwargs

Python gives you several ways to pass arguments, and knowing them makes functions both easier to call and easier to read at the call site. This chapter builds on the previous one.

Default values

A parameter written as exponent=2 has a default: if the caller leaves it out, Python uses 2. Parameters with defaults must come after those without. Defaults turn the common case into a short call, power(5), while keeping the general case available, power(2, 5).

Keyword arguments

Any argument can be passed by name: power(exponent=3, base=2). Named arguments may appear in any order and make calls self-documenting, especially for booleans: save(data, overwrite=True) is clearer than save(data, True). Positional arguments must come before keyword arguments in a call.

Collecting extra arguments

  • A parameter written *numbers collects any extra positional arguments into a tuple. That is how total(1, 2, 3) accepts any count.
  • A parameter written **details collects extra keyword arguments into a dictionary.

The names args and kwargs are only conventions; the stars are what matter. The same stars work in reverse at a call: power(*pair) spreads a sequence into positional arguments and f(**options) spreads a dict into keyword arguments.

Python 3 · editable

Expected output

25
32
8
<class 'tuple'> (1, 2, 3)
6
Emma {'city': 'Austin', 'joined': 2025}
['pen'] ['ink']

The mutable default trap

Default values are evaluated once, when the def statement runs, not on every call. If the default is a mutable object such as a list, every call that relies on it shares the same list, and items pile up between calls. The fix, shown in add_item, is to use None as the default and create a fresh list inside the function. The output shows each call getting its own list.

Common mistakes

  • Using [] or {} as a default value.
  • Putting a parameter without a default after one with a default, which is a SyntaxError.
  • Passing the same argument twice, once by position and once by name, which raises TypeError.

Summary

  • param=value sets a default used when the caller omits the argument.
  • Keyword arguments make calls explicit and order-independent.
  • *args gathers extra positional arguments in a tuple; **kwargs gathers extra keyword arguments in a dict.
  • Use None instead of a mutable default.

Quiz: Default and keyword arguments

1. With def f(a, b=10): return a + b, what is f(1)?

b falls back to its default 10.

2. Inside def g(*args):, what type is args?

Extra positional arguments are collected into a tuple.

3. Why is def add(x, items=[]): risky?

Defaults are created once when the function is defined.

4. Which call is valid for def p(base, exponent=2):?

Positional arguments must come before keyword arguments, and base is required.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.