How to Use Default Function Arguments in Python?

In one of my projects for my New York clients, I came across a scenario where I needed to use the default function arguments in Python, then I explored more on this topic and I will share my find findings in this tutorial with suitable examples and screenshots.

Use Default Function Arguments in Python

In Python, you can assign default values to function parameters. If an argument is not provided when the function is called, the parameter will automatically take on the specified default value. Here’s a simple example:

def greet(name="John"):
    print(f"Hello, {name}!")

greet()
greet("Sarah") 

Output:

Hello, John!
Hello, Sarah!

You can look at the output in the screenshot below.

Use Default Function Arguments in Python

In this case, if no argument is passed to the greet() function, it will use the default value of "John". However, you can still provide a different argument like "Sarah" overriding the default.

Default arguments can make your functions more flexible and reduce duplication. For instance, let’s say you’re building an e-commerce app that needs to calculate sales tax. You could define a function with a default tax rate:

def calculate_total(price, tax_rate=0.08):
    return price * (1 + tax_rate)

print(calculate_total(100))
print(calculate_total(100, 0.07)) 

Output:

108.0
107.0

You can look at the output in the screenshot below.

How to Use Default Function Arguments in Python

Here, the tax_rate parameter defaults to 8% (0.08) which is a common rate in many US states. But the function allows passing a different rate when needed.

Read How to Use the arange() Function in Python?

Beware the Mutable Default Argument

One important gotcha with default arguments is that Python evaluates them only once when the function is defined, not each time it’s called. This matters for mutable objects like lists and dictionaries. For example:

def add_user(users=[]): 
    users.append("John")
    print(users)

add_user()  
add_user()

Output:

['John']
['John', 'John']

You can look at the output in the screenshot below.

Use Default Function Arguments in Python Mutable Default Argument

Whoa, the second call to add_user() remember the change from the first call! That’s because the empty list [] was created when the function was defined and is reused each time.

To avoid this surprising behavior, a common idiom is to use None as the default and create the mutable object inside the function:

def add_user(users=None):
    if users is None:
        users = []
    users.append("John") 
    print(users)

Now each call to add_user() will create a new, independent list.

Check out How to Use the insert() Function in Python?

Keyword Arguments and Defaults

You can combine default values with keyword arguments for even more expressive function calls. Keyword arguments let you specify which parameter you’re providing a value for. Continuing our sales tax example:

def calculate_total(price, tax_rate=0.08, discount=0):
    subtotal = price * (1 - discount)
    return subtotal * (1 + tax_rate) 

print(calculate_total(100, discount=0.1))
# Output: 97.2

Here the discount defaults to 0, but we can apply a 10% discount by passing discount=0.1 , while still using the default 8% tax rate.

Read How to Use the strip() Function in Python?

Conclusion

In this tutorial, I have explained how to use the default function arguments in Python. I discussed using the default function argument in Python, beware of the multiple arguments, and keyword arguments and defaults.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.