Lesson explanation
After this lesson, you will be able to:
A default argument gives a parameter a value when the caller omits that argument:
def format_status(service, status="unknown"):
return f"{service}: {status}"format_status("worker") uses "unknown", while format_status("worker", "healthy") overrides it.
Parameters without defaults must come before parameters with defaults:
def create_job(name, retries=3):
...Python also lets callers pass arguments by name:
create_job(name="backup", retries=5)Keyword arguments make calls clearer when several values have similar types or when only one optional setting needs an override. Their order can differ from the parameter order, as long as every name is valid and no argument is supplied twice.
Function tests can verify default behavior by calling a function with fewer arguments. The current function-task runner calls functions positionally, so this lesson practices keyword-call syntax through focused code tasks while using real function tests for the function behavior.
Advanced topics such as *args, **kwargs, keyword-only parameters, and mutable default pitfalls belong in the later Functions Deep Dive unit.
💡 Tip: Defaults should represent a safe, unsurprising common case—not a value chosen only to make one example shorter.
def cache_label(service, ttl=300):
return f"{service}: {ttl}s"
print(cache_label("api"))Output:
api: 300sThe caller supplies only the required argument.
def retry_label(job, retries=3):
return f"{job}: {retries} retries"
print(retry_label("sync", 5))Output:
sync: 5 retriesThe second argument replaces the default value.
def build_window(name, width=800, height=600):
return f"{name}: {width}x{height}"
print(build_window(name="Dashboard", height=720, width=1280))Output:
Dashboard: 1280x720Keyword arguments may be supplied in a different order.
def create_alert(message, level="info", channel="console"):
return f"[{level}] {channel}: {message}"
print(create_alert("Queue delayed", channel="slack"))Output:
[info] slack: Queue delayedOnly the channel is overridden; the level keeps its default.
Python for Developers
Assembled code
Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.
0 / 6 tasks completed
| Goal | Python |
|---|
| Define an optional parameter | parameter=value |
| Use the default | Omit that argument |
| Override the default | Supply another value |
| Make a call self-explanatory | parameter=value in the call |
| Keep a valid signature | Required parameters before defaults |