Default Arguments
📚 What are Default Arguments? Default arguments let you specify a fallback value for a function parameter. If the caller does not provide that argument, the default is used. This makes functions more flexible: callers can omit optional inputs, keeping calls short while still allowing full control …

Appy Says…
Ever notice how print() works whether you pass one thing or five? Default arguments let your functions work the same way — useful with minimal input, flexible with more. This makes your functions feel professional and polished.
What are Default Arguments?
A default argument is a parameter with a preset value. If the caller doesn't provide that argument, Python uses the default. Default parameters must come after required parameters in the function signature.
- •Syntax:
def greet(name, greeting='Hello'): - •Call with default:
greet('Appy')→Hello Appy - •Call with override:
greet('Appy', 'Hey')→Hey Appy - •Keyword arguments:
greet(name='Appy', greeting='Yo') - •Defaults must be immutable values (strings, numbers,
None) — never a list or dict - •
*argscollects extra positional args into a tuple;**kwargscollects extra keyword args into a dict
Think of it like ordering a game skin
When you buy a Roblox game, it gives you a default character skin. If you have a custom skin, you can use that instead. Default arguments are like that default skin — the function works perfectly without any input, but you can always customise it when you want something different.
How It Works
- •1. Define the function with a default:
def connect(host, port=8080): - •2. Python stores the default value when the
defline runs - •3. When you call
connect('localhost'), Python seesportis missing and uses8080 - •4. When you call
connect('localhost', 443), Python uses443 - •5. Keyword arguments allow any order:
connect(port=443, host='example.com') - •6. The classic trap: mutable defaults like
def add(item, lst=[])— the list persists across calls!
Real-World Examples
- •A game difficulty function:
def start_game(player, difficulty='medium'): - •A message sender:
def send(text, sender='Appy', colour='blue'): - •Python's own
print(end='\n', sep=' ')uses defaults for the newline and separator - •A database connector:
def connect(host, port=5432, timeout=30): - •A social app profile:
def create_profile(username, bio='No bio yet', private=False):
Key Facts
- •Default values are evaluated once at function definition time, not each call — this is why mutable defaults are dangerous
- •The safe pattern for a mutable default is:
def add(item, lst=None): if lst is None: lst = [] - •
**kwargsis how Python's owndict(),open(), and most libraries accept flexible named options - •PEP 8 recommends no spaces around the
=in default values:def f(x=1)
Watch Out!
Never use a mutable object (list, dict) as a default argument. def add(item, lst=[]) reuses the same list every call, so items accumulate across calls. Use def add(item, lst=None): if lst is None: lst = [] instead.
Remember
Default arguments make functions flexible and easy to call. Put required parameters first, defaults last. Use None as the default when the real default should be a mutable object.
What You Learned
- •Default arguments let parameters have preset values:
def f(x, y=10): - •Never use mutable defaults (list/dict) — use
Noneand create inside the function - •Unlocks: flexible APIs, optional config, cleaner function calls throughout your codebase
Key Facts
- →Default values are evaluated once at function definition time, not each call — this is why mutable defaults are dangerous
- →The safe pattern for a mutable default is:
def add(item, lst=None): if lst is None: lst = [] - →
**kwargsis how Python's owndict(),open(), and most libraries accept flexible named options - →PEP 8 recommends no spaces around the
=in default values:def f(x=1)
Real-World Examples
Remember
Default arguments make functions flexible and easy to call. Put required parameters first, defaults last. Use None as the default when the real default should be a mutable object.
Quick Quiz
Default args are used when?