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.