Lesson 23 of 34
Default & Keyword Arguments
8 min 30 XP
Give a parameter a default value. If the caller leaves it out, the default is used.
Example: edit me and press Run
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.Keyword arguments: pass values by name and the order no longer matters. It also makes code easier to read.
Example: edit me and press Run
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.Parameters with defaults must come after those without: def f(a, b=2) is fine, def f(a=1, b) is an error.