How to Use Default Arguments in Python

Default arguments let you give a function parameter a starting value in the function definition.

This means a function can work in two ways:

  • with the argument you pass in
  • or with a built-in fallback value

This is useful when one value is common, but you still want the option to change it.

Quick answer

def greet(name, message="Hello"):
    print(message, name)

greet("Sam")
greet("Sam", "Hi")

Output:

Hello Sam
Hi Sam

Set the default value in the function definition. If no value is passed for that parameter, Python uses the default.

What default arguments do

A default argument gives a parameter a value before the function is called.

That means:

  • the caller can skip that argument
  • the function still works
  • the caller can override the default by passing a different value

This is helpful when a function usually uses the same value.

For example, many greeting messages might use "Hello" most of the time, but sometimes you want "Hi" or "Welcome" instead.

If you are new to functions, see Python functions explained first.

Basic syntax

Write the default directly in the function definition.

def func(a, b=10):
    print(a, b)

In this example:

  • a is required
  • b is optional
  • if no value is given for b, Python uses 10

Example calls:

def func(a, b=10):
    print(a, b)

func(5)
func(5, 20)

Output:

5 10
5 20

A good rule is:

  • required parameters first
  • default parameters after them

This matches how function parameters and arguments in Python work.

Create a function with one default argument

Here is a simple example with one default value:

def greet(name, message="Hello"):
    print(message, name)

greet("Maya")
greet("Maya", "Welcome")

Output:

Hello Maya
Welcome Maya

What happens here:

  • name is required
  • message has a default value of "Hello"
  • greet("Maya") uses the default
  • greet("Maya", "Welcome") replaces the default

This makes the function flexible without making every call longer.

Use multiple default arguments

A function can have more than one default parameter.

This is useful when several settings are optional.

def show_text(text, separator="-", ending="!"):
    print(text + separator + ending)

show_text("Hello")
show_text("Hello", "*", "?")
show_text("Hello", ending=".")

Output:

Hello-!
Hello*?
Hello-.

Here:

  • separator defaults to "-"
  • ending defaults to "!"

You can call the function:

  • with only the required argument
  • by changing both defaults
  • or by changing just one of them

Use keyword arguments with defaults

Keyword arguments make function calls easier to read.

They are especially useful when a function has several optional parameters.

def print_info(name, age=0, city="Unknown"):
    print("Name:", name)
    print("Age:", age)
    print("City:", city)

print_info("Sam")
print_info("Sam", city="London")
print_info("Sam", age=25, city="Paris")

Output:

Name: Sam
Age: 0
City: Unknown
Name: Sam
Age: 0
City: London
Name: Sam
Age: 25
City: Paris

Why keyword arguments help:

  • they make the call clearer
  • you can override only the value you want
  • you do not need to remember the exact position of every optional argument

Rules beginners should remember

Keep these rules in mind when using default arguments:

  • Required parameters should come before default parameters.
  • A parameter with a default value is optional.
  • If you pass a value, it replaces the default.
  • Default values are set when the function is defined.
  • Avoid using changing objects like lists or dictionaries as defaults.

This is also important when writing functions that return values in Python functions, because clear parameters make functions easier to reuse.

Why mutable default values can cause bugs

One common beginner mistake is using a list or dictionary as a default value.

This looks harmless:

def add_item(item, items=[]):
    items.append(item)
    return items

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

Output:

['a']
['a', 'b']

Many beginners expect the second call to return just ['b'].

But the same list is reused across calls.

Safer version

Use None as the default, then create a new list inside the function.

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

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

Output:

['a']
['b']

This is the safe pattern for optional lists and dictionaries.

Common errors and how to fix them

1. Required parameter after a default parameter

This causes a syntax error.

Wrong:

def greet(message="Hello", name):
    print(message, name)

Python does not allow this because required parameters must come first.

Correct:

def greet(name, message="Hello"):
    print(message, name)

2. Passing too many arguments

If a function only accepts a certain number of arguments, extra values raise a TypeError.

def greet(name, message="Hello"):
    print(message, name)

greet("Sam", "Hi", "Extra")

You would get an error because the function expects only one or two arguments.

If you see this kind of problem, a page like fix TypeError: missing required positional argument may also help with argument-related errors.

3. Using the wrong keyword name

Keyword names must match the parameter names exactly.

Wrong:

def print_info(name, age=0):
    print(name, age)

print_info("Sam", years=25)

This raises a TypeError because years is not a parameter name.

Correct:

def print_info(name, age=0):
    print(name, age)

print_info("Sam", age=25)

For more help with this specific problem, see fix TypeError: unexpected keyword argument.

4. Mutable defaults keeping old data

If a list or dictionary is changed inside the function, that changed object may be reused next time.

Use None instead of [] or {} as the default.

Common mistakes

These are the most common causes of problems with default arguments:

  • Forgetting that default values make parameters optional
  • Putting non-default parameters after default parameters
  • Using a list or dictionary as a default value
  • Mixing positional and keyword arguments incorrectly
  • Assuming the default value is recreated on every call

If something looks wrong, these checks can help:

help(function_name)
print(function_name.__defaults__)
print(function_name.__code__.co_varnames)
print(type(value))

What these do:

  • help(function_name) shows how the function should be called
  • function_name.__defaults__ shows the current default values
  • function_name.__code__.co_varnames shows the function's variable names
  • type(value) helps confirm the kind of value you passed

FAQ

What is a default argument in Python?

It is a parameter value written in the function definition that Python uses when no argument is provided for that parameter.

Can a function have more than one default argument?

Yes. A function can have several optional parameters with default values.

Why should required parameters come first?

Python syntax requires parameters without defaults to come before parameters with defaults.

Why is using a list as a default argument a problem?

Because the same list can be reused across calls, which can keep old changes and cause unexpected results.

How do I safely use a list as an optional argument?

Use None as the default, then create a new list inside the function if needed.

See also