Lesson 20 of 60
โฎPrev Nextโฏ

PY-101 ยท Functions

Default and keyword arguments

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

20of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A default value makes a parameter optional: def greet(name, greeting="Hello"): uses "Hello" unless you pass something else. Keyword arguments name the parameter in the call, such as greet("Asha", greeting="Hi"), so order no longer matters and the call explains itself.

    • Set default values
    • Call with keyword arguments
    • Avoid mutable defaults

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Default and keyword arguments, running
    1. def make_pizza(topping, size="medium", crust="regular"):
    2. return f"{size} {topping} pizza, {crust} crust"
    3. print(make_pizza("paneer"))
    4. print(make_pizza("corn", size="large"))
    5. print(make_pizza(crust="thin", topping="veg"))
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA pizza order has a default size (medium) and crust (regular). Most people only say the topping; the shop fills in the rest unless told otherwise.
    2. The Python ideadef name(required, option="default"):A default value makes a parameter optional: def greet(name, greeting="Hello"): uses "Hello" unless you pass something else.
    3. In real lifeprint itself has defaults: sep=" " and end="\n". You have been using keyword arguments since your first lesson.

    Start with a question

    A pizza order has a default size (medium) and crust (regular). Most people only say the topping; the shop fills in the rest unless told otherwise.

    Libraries use defaults everywhere so beginners can call them simply and experts can still tune every option.

    Explain it like I'm 10

    Defaults are the settings a game starts with. You only change the ones you care about.

    The technical explanation

    Parameters with defaults must come after those without: def f(a, b=2): is fine, def f(a=1, b): is a syntax error.

    Keyword arguments use the parameter name: make_pizza("paneer", size="large").

    Positional arguments must come before keyword arguments in a call.

    Syntax

    def name(required, option="default"):
        ...
    
    name(1)
    name(1, option="other")

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    print itself has defaults: sep=" " and end="\n". You have been using keyword arguments since your first lesson.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. def f(a, b=3): return a + b. What is f(2)?
    2. 2. Which definition is valid?
    3. 3. What is a keyword argument?
    4. 4. Why avoid def f(items=[])?
    5. 5. print("a", "b", sep="-") uses what?

    Questions people ask

    Can I pass arguments in any order in Python?

    Yes, if you pass them as keyword arguments by name. Positional arguments must follow the order of the parameters.

    Why is a mutable default argument a bug?

    Default values are created once when the function is defined. A default list is shared by every call, so items pile up. Use None and create a new list inside.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Can I pass arguments in any order in Python?

    Yes, if you pass them as keyword arguments by name. Positional arguments must follow the order of the parameters.

    beginner Why is a mutable default argument a bug?

    Default values are created once when the function is defined. A default list is shared by every call, so items pile up. Use None and create a new list inside.

    intermediate def f(a, b=3): return a + b. What is f(2)?

    5. b defaults to 3.

    intermediate Which definition is valid?

    def f(a, b=1):. Defaults go after required parameters.

    Quick revision

    Remember

    • Set default values
    • Call with keyword arguments
    • Avoid mutable defaults
    • Never use a list or dict as a default, like def f(items=[]). Python reuses the same list on every call. Use None and create the list inside.

    Syntax

    def name(required, option="default"):
        ...
    
    name(1)
    name(1, option="other")

    Official documentation