Python, from Zero, for AILesson 22 of 89

Module 3 · Functions, modules, and code you can read again in March

Arguments: positional, named, default, and the trap in the default

In this lesson

  • 9 min read
  • 5 reading sections
  • Knowledge check
  • Lesson 2 of 9 in this chapter

This chapter’s goalSplit a working script into functions and modules with explicit arguments, return values and docstrings, explain from Python’s scoping rules why a variable changed inside a function did or did not change outside it, and run the result from a terminal with named options.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:44Not available
Full audiobookNot availableNot available
One list in a def line, four callsImportdef add(item, basket=[]) runs. One empty list is created, now and never again.Call 1add("milk") uses the default. Returns ['milk'].Call 2add("rice") uses the same list. Returns ['milk', 'rice']: the surprise.Call 3add("dal", []) passes a fresh list, so the default is untouched. Returns ['dal'].Call 4add("atta") is back on the default, which still holds two items. Returns three.The list was created once, when the def statement ran, and it belongs to the function rather than toany call. The fix is always the same: default to None, and make the real list inside the body.
The list was created once, when the def statement ran, and it belongs to the function rather than to any call. The fix is always the same: default to None, and make the real list inside the body.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 7Free · no account needed
Lesson steps

Four ways to hand a value to a function

Python
def send(message, to, retries=3, urgent=False):
    ...

send("hello", "asha@example.com")                    # positional
send("hello", to="asha@example.com")                 # mixed
send(to="asha@example.com", message="hello")         # all named, any order
send("hello", "asha@example.com", 5, True)           # all positional

Positional arguments are matched by order. Named ones are matched by name and can appear in any order, but every positional argument must come before every named one, or you get SyntaxError: positional argument follows keyword argument.

That last call, send("hello", "asha@example.com", 5, True), is legal and bad. Six months later nobody knows what 5 and True mean. Passing booleans and bare numbers by name (retries=5, urgent=True) costs eight characters and removes a whole category of misreading.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Arguments: positional, named, default, and the trap in the default’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/arguments-in-depth (Accessed: 27 November 2026).