Paul's Python CoursePPC

Default arguments

Not met yet

A parameter can have a default: def label(item, copies=1):. When a call leaves that argument out, the function uses the default; when the call passes one, the default is ignored. Parameters with defaults go after the ones without.

Try it

main.py
def label(item, copies=1):
    return f"{item} x{copies}"

print(label("kettle"))
print(label("clock", 3))

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks a parameter with a default must still be passed in every call.
  • Thinks a left-out argument becomes None rather than the default in the def line.
  • Thinks the default is used even when the call passes a value.

Where it comes up