Python · Stage 6 – Reusable Code · Lesson 35 of 50

Default Parameter Values

Give a parameter a value used only when the caller omits it.

def greet(name="friend"):
    print(f"Hello, {name}")
Line-by-line explanation
  1. The function has one parameter.
  2. ="friend" gives it a default.
  3. The body displays the selected name.
greet()
Line-by-line explanation
  1. The call provides no name.
  2. Python uses friend.
  3. The output is Hello, friend.
greet("Maya")
Line-by-line explanation
  1. The call provides Maya.
  2. Maya replaces the default for this call.
  3. The output is Hello, Maya.

Tiny Practice

Give the language parameter a default of Python.

Starter template

def show_language(language=CHANGE_ME):
    print(language)

show_language()
Line-by-line explanation
  1. The function has one parameter.
  2. The call provides no argument.
Hint
  1. Use a string.
  2. Put Python in quotes.
  3. Keep the equals sign.
Show Solution
def show_language(language="Python"):
    print(language)

show_language()
Line-by-line explanation
  1. The parameter defaults to Python.
  2. The empty call uses that default.
  3. Python is displayed.