🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
🐍 Python

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 …

8 min 10 XP Lesson 15 of 21
Default Arguments
🌐

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
  • *args collects extra positional args into a tuple; **kwargs collects 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 def line runs
  • 3. When you call connect('localhost'), Python sees port is missing and uses 8080
  • 4. When you call connect('localhost', 443), Python uses 443
  • 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 = []
  • **kwargs is how Python's own dict(), 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 None and 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 = []
  • **kwargs is how Python's own dict(), 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

• A game difficulty function: <code>def start_game(player, difficulty='medium'):</code> • A message sender: <code>def send(text, sender='Appy', colour='blue'):</code> • Python's own <code>print(end='\n', sep=' ')</code> uses defaults for the newline and separator • A database connector: <code>def connect(host, port=5432, timeout=30):</code> • A social app profile: <code>def create_profile(username, bio='No bio yet', private=False):</code>

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

1 / 2

Default args are used when?