Hintor
Python Basics for Programmers

Default and Keyword Arguments

Python for Developers/Python Basics for Programmers/

Default and Keyword Arguments

Python

Lesson explanation

Default and Keyword Arguments


🎯 Learning Objective

After this lesson, you will be able to:

  • Define optional parameters with safe default values.
  • Call the same function with omitted defaults or explicit overrides.
  • Use keyword arguments to make multi-parameter calls easier to read.
  • Order required and default parameters correctly in a function signature.

Short Explanation

A default argument gives a parameter a value when the caller omits that argument:

Python
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:

Python
def create_job(name, retries=3):
    ...

Python also lets callers pass arguments by name:

Python
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.


Clean Code Examples

1. Use a default when a value is omitted

Python
def cache_label(service, ttl=300):
    return f"{service}: {ttl}s"

print(cache_label("api"))

Output:

TEXT
api: 300s

The caller supplies only the required argument.

2. Override the default

Python
def retry_label(job, retries=3):
    return f"{job}: {retries} retries"

print(retry_label("sync", 5))

Output:

TEXT
sync: 5 retries

The second argument replaces the default value.

3. Use keyword arguments for clarity

Python
def build_window(name, width=800, height=600):
    return f"{name}: {width}x{height}"

print(build_window(name="Dashboard", height=720, width=1280))

Output:

TEXT
Dashboard: 1280x720

Keyword arguments may be supplied in a different order.

4. Keep required parameters first

Python
def create_alert(message, level="info", channel="console"):
    return f"[{level}] {channel}: {message}"

print(create_alert("Queue delayed", channel="slack"))

Output:

TEXT
[info] slack: Queue delayed

Only the channel is overridden; the level keeps its default.



⚠️ Common Mistakes

  1. Do not place a required parameter after a default parameter.
  2. Do not provide the same argument both positionally and by keyword.
  3. Do not misspell a keyword argument; it must match the parameter name.
  4. Do not use a confusing default that changes the function's meaning unexpectedly.
Start Practice

Python for Developers

Default and Keyword Arguments

6 tasks
Lesson progress0 / 6 tasks completed

Tasks

Practice progress

Complete the default retry parameter

View task

Complete the optional retry parameter so callers may omit it.

Requirements

  • Keep the function name, required parameter, and body unchanged.
  • Fill only the missing parameter definition.

Expected output

nightly-sync: 3 retries
Code: PythonTask 1

Complete the default retry parameter

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3
4
5

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 6 tasks completed

Previous lessonBack to unitBack to track
GoalPython
Define an optional parameterparameter=value
Use the defaultOmit that argument
Override the defaultSupply another value
Make a call self-explanatoryparameter=value in the call
Keep a valid signatureRequired parameters before defaults