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.

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.0You can look at the output in the screenshot below.

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.

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.2Here 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:
- How to Use the trim() Function in Python?
- How to Use the Floor() Function in Python?
- How to Use the round() Function in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.