Code

Sensible Defaults

A parameter can have a default value. If the caller leaves that argument out, the default is used.

def brew(potion, doses=1):
    return f"{doses} x {potion}"

brew("glow")       # '1 x glow'
brew("glow", 3)    # '3 x glow'

Parameters with defaults must come after the ones without.

Keyword arguments

You can also pass arguments by name. Then the order doesn't matter, and the call explains itself:

brew(doses=2, potion="calm")   # '2 x calm'

Assignment

  1. greet_traveler(name, greeting="Welcome") returns "<greeting>, <name>!": e.g. greet_traveler("Ada") gives "Welcome, Ada!".
  2. fuel_for(nights, per_night=3) returns nights * per_night.