PY-101 ยท Functions
Default and keyword arguments
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
def make_pizza(topping, size="medium", crust="regular"):return f"{size} {topping} pizza, {crust} crust"print(make_pizza("paneer"))print(make_pizza("corn", size="large"))print(make_pizza(crust="thin", topping="veg"))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
medium paneer pizza, regular crust
large corn pizza, regular crust
medium veg pizza, thin crust ๐ฏ Write price_with_tax(amount, rate=18) that returns amount plus rate percent. price_with_tax(100) should be 118.0 and price_with_tax(100, rate=5) should be 105.0.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
def price_with_tax(amount, rate=18): return amount + amount * rate / 100
- 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.
- The Python idea
def name(required, option="default"):A default value makes a parameter optional: def greet(name, greeting="Hello"): uses "Hello" unless you pass something else. - 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") Real-world scenario
print itself has defaults: sep=" " and end="\n". You have been using keyword arguments since your first lesson.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: *args and **kwargs โ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. UseNoneand create the list inside.
Syntax
def name(required, option="default"):
...
name(1)
name(1, option="other") Official documentation
- Default argument values (docs.python.org)