🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Default Parameter Value

AI & DATA SCIENCE // default-parameter-value

A default parameter value lets a function argument be optional, supplying a fallback value used whenever the caller doesn't provide one explicitly.

Syntax

def func(param=default_value):
    ...

func()          # uses default_value
func(other)     # overrides it

Deep Dive Course

Writing a parameter with an equals sign and a value in a function definition makes that parameter optional — if the caller omits it, the function uses the given default instead. Default values are evaluated exactly once, at the moment the function is defined, not on every call, which is why using a mutable object like a list or dict as a default is a classic bug: that same object is shared and can accumulate state across calls that don't override it.

1Understanding Default Parameter Value

Writing a parameter with an equals sign and a value in a function definition makes that parameter optional — if the caller omits it, the function uses the given default instead. Default values are evaluated exactly once, at the moment the function is defined, not on every call, which is why using a mutable object like a list or dict as a default is a classic bug: that same object is shared and can accumulate state across calls that don't override it.

💡

Never use a mutable object, like an empty list or dict, as a default parameter value — use None as the default instead, and create a fresh mutable object inside the function body when it's actually needed.

editor.html
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", "Hi"))
localhost:3000

2Practical Example

Here is a real-world application of Default Parameter Value showing how it is used in production Python code.

editor.html
def add_item(item, bucket=None):
    if bucket is None:
        bucket = []
    bucket.append(item)
    return bucket

print(add_item("a"))
print(add_item("b"))
localhost:3000

3Best Practices

Follow these guidelines when working with Default Parameter Value:

1. Use None as the default for optional mutable arguments, creating the real object inside the function body

2. Put parameters with defaults after all parameters without defaults, since Python's syntax requires that order

3. Choose defaults that represent a sensible, safe fallback, not just an arbitrary placeholder value

⚠️

Tip: Never use a mutable object, like an empty list or dict, as a default parameter value — use None as the default instead, and create a fresh mutable object inside the function body when it's actually needed.

editor.html
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", "Hi"))
localhost:3000

Examples

Example 01Basic Usage
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", "Hi"))
Example 02Advanced Example
def add_item(item, bucket=None):
    if bucket is None:
        bucket = []
    bucket.append(item)
    return bucket

print(add_item("a"))
print(add_item("b"))

Best Practices

  • Use None as the default for optional mutable arguments, creating the real object inside the function body
  • Put parameters with defaults after all parameters without defaults, since Python's syntax requires that order
  • Choose defaults that represent a sensible, safe fallback, not just an arbitrary placeholder value

Interview Question

Why does using a mutable default value, like an empty list, cause a function's behavior to change unexpectedly across multiple calls?

Hint: Think about when the default value expression is actually evaluated.

Default parameter values are evaluated exactly once, when the def statement runs and the function object is created, not each time the function is called. If that default is a mutable object, like a list, every call that doesn't override the parameter shares that same single object, so mutating it in one call, like appending to it, leaves the change visible on the next call too. Using None as the default and creating a fresh object inside the function body avoids this shared-state trap.

Exercises

MediumPractice using Default Parameter Value in a real scenario.
View Solution
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", "Hi"))

Frequently Asked Questions

Why does using a mutable default value, like an empty list, cause a function's behavior to change unexpectedly across multiple calls?

Default parameter values are evaluated exactly once, when the def statement runs and the function object is created, not each time the function is called. If that default is a mutable object, like a list, every call that doesn't override the parameter shares that same single object, so mutating it in one call, like appending to it, leaves the change visible on the next call too. Using None as the default and creating a fresh object inside the function body avoids this shared-state trap.

Related Functions

nonetypedef-keywordarguments-args