Default and Keyword Arguments

A default value lets a call leave an argument out, a keyword argument names the parameter it fills so the order no longer matters, and print() uses both in its sep= and end= arguments.

Apprentice · Python · Updated

By

After this lesson you can

  • Give a parameter a default value, and call a function with keyword arguments
  • Change what print() puts between and after its values with sep= and end=

Key takeaways

  • A default value, written goal=10000 in the def line, is what the parameter gets when a call leaves that argument out.
  • Parameters with a default come after the ones without, or Python refuses to run the file.
  • A keyword argument names the parameter it fills, such as goal=6000, so it can skip a default and be written in any order after the positional arguments.
  • print() has keyword arguments of its own. sep= sets what goes between its values, and end= what goes after them, a new line unless you change it.

Most calls to a function pass the same value for some argument: the usual tax rate, the standard shipping, the normal step goal. Writing it out every time is noise, and it hides the one call that's different. Default values let a call leave the usual value out, and keyword arguments let a call say exactly which value is which. You'll use both every day, because Python's own functions, print() among them, are built this way.

Stride, the step-counter app, gives everyone the standard goal of 10,000 steps a day, and lets a walker set their own.

The same argument, every call

Here's how far each walker got toward their goal, as a percentage:

def percent_of_goal(steps, goal):
    return round(steps / goal * 100)


print(percent_of_goal(8400, 10000))
print(percent_of_goal(5200, 10000))
print(percent_of_goal(4500, 6000))
84
52
75

Maya and Rosa have the standard goal, and Leo has set his own, 6,000. Nearly every call says 10000. You could take the goal parameter out and write 10000 inside the function, but then Leo's 4,500 steps would come out as 45% instead of 75%. The function needs the goal to be optional: the standard one unless a call says otherwise.

Default values

A form often fills in a blank for you: Country: United States, which you change only if it's wrong. A default value is that pre-filled blank. You write it in the def line with =, and goal=10000 there is read "goal, which defaults to 10000":

def percent_of_goal(steps, goal=10000):
    return round(steps / goal * 100)


print(percent_of_goal(8400))
print(percent_of_goal(4500, 6000))
84
75

The first call leaves goal out, so it's 10000. The second gives 6000, which replaces the default for that call only.

Parameters with a default go after the ones without. Write def percent_of_goal(goal=10000, steps): and Python refuses to run the file with SyntaxError: parameter without a default follows parameter with a default, because it couldn't tell which value a one-argument call meant for which parameter.

Keyword arguments: saying which is which

With two or three arguments, position works. With more, a call like progress_line("Ana", 3, 5, "miles") makes the reader count. A keyword argument names the parameter it fills: goal=5 in a call means "this value goes to goal". Keyword arguments can come in any order, and they can skip a default to reach the one after it:

def progress_line(name, steps, goal=10000, label="steps"):
    return f"{name}: {steps} of {goal} {label}"


print(progress_line("Maya", 8400))
print(progress_line("Leo", 4500, 6000))
print(progress_line("Ana", 3, label="miles", goal=5))
print(progress_line("Rosa", 5200, label="steps today"))
Maya: 8400 of 10000 steps
Leo: 4500 of 6000 steps
Ana: 3 of 5 miles
Rosa: 5200 of 10000 steps today

Rosa's call gives label but leaves goal at its default, something position alone can't do. The plain arguments, matched by their place in the call, are positional arguments, and they come first: a call such as progress_line(name="Sam", 9100) stops with SyntaxError: positional argument follows keyword argument.

print()'s own keyword arguments: sep and end

print() is a function too, and it takes as many values as you give it. Its definition has two parameters with defaults that you can change with keyword arguments:

  • sep, the separator: what goes between the values. It defaults to one space, " ".
  • end: what goes after the last value. It defaults to "\n" (read "new line"), which is why each print() starts a new line.
print("Mon", "Tue", "Wed")
print("Mon", "Tue", "Wed", sep=" | ")
print("Steps today:", end=" ")
print(8400)
Mon Tue Wed
Mon | Tue | Wed
Steps today: 8400

With end=" ", the first print() ends with a space instead of a new line, so the next one carries on along the same line.

Reading other people's code, you'll meet *args and **kwargs in a def line: they let a function take any number of positional or keyword arguments, which is how print() takes as many values as you give it.

Do it: write Stride's walk reminder

Every evening, Stride reminds each walker to take a walk. Most walkers keep the standard reminder, a 30-minute walk at 6 pm, and some change the length, the time, or both.

  • name: the walker's name.
  • minutes: how long the walk is, 30 unless the call gives another number.
  • time: when to walk, "6 pm" unless the call gives another time.

Your job: write the function walk_reminder(name, minutes=30, time="6 pm") so it returns the reminder as text: the name, a comma, then time for a <minutes>-minute walk at <time>.

  • It returns the text; it doesn't print it.
  • Leaving out minutes, time or both uses their defaults.

Worked through: walk_reminder("Maya") leaves out both, so minutes is 30 and time is "6 pm", and it returns "Maya, time for a 30-minute walk at 6 pm.". walk_reminder("Ana", time="7 am") keeps the 30 minutes and returns "Ana, time for a 30-minute walk at 7 am.".

Practiced live on the lesson page: write the Python right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

Defaults keep the usual call short and keyword arguments keep the unusual one clear, so a function can grow new options without breaking a single call that already uses it.

Key terms

Related