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.

Challenge

+30 XP

Write price(amount, discount=10) that returns the final price after the percentage discount: amount - amount * discount // 100 Example: price(1000) → 900, price(1000, 20) → 800

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.