Python · Intermediate · 11 min
Flexible functions
Call functions with default values and keyword arguments.
Provide a default
A default parameter is used when the caller omits that argument. Required parameters come before parameters with defaults.
def greet(name, greeting="Hello"):
return f"{greeting}, {name}"
print(greet("Ada"))Output
Hello, Ada
Name an argument
Keyword arguments make the meaning of a call explicit and can be supplied in a different order.
def price(amount, tax=0.2):
return round(amount * (1 + tax), 2)
print(price(tax=0.1, amount=50))Output
55.0
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Format a label
Complete label so prefix defaults to ID. Display ID-42 and USER-7 using one positional call and one keyword argument.
Build the returned text from prefix and number.
Flexible functions
- Read both flexible functions examples before answering.
- Run the task, compare its exact output, and revise the code if needed.