6.2.2 Default Parameter Values

Unit: Unit 6: Functions • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Define functions with optional parameters and default values
def format_currency(amount, currency_symbol="$", decimal_places=2):
    formatted = f"{currency_symbol}{amount:.{decimal_places}f}"
    return formatted

# 1. Use all default values
standard = format_currency(49.5)

# 2. Override currency symbol
euro = format_currency(85.0, currency_symbol="€")

# 3. Override both symbol and decimal precision
crypto = format_currency(0.004821, "BTC ", decimal_places=6)

print("Standard:", standard)
print("Euro:", euro)
print("Crypto:", crypto)
Open in Live Interactive Python Sandbox →