Last Updated: January 3, 2026
Understanding how to use default arguments in Python can transform the way you write functions. It simplifies code by allowing certain parameters to have preset values, which can save time and reduce errors when calling those functions.
So, let's dive into what default arguments are, how they work, and some best practices to keep in mind.
Default arguments are predefined values that a function uses if no arguments are provided during the function call. This feature allows for more flexible and readable code.
When you define a function, you can specify default values for any parameters. If the caller does not provide a value for those parameters, Python uses the default values instead.
Here's a simple example to illustrate this:
In this example, the greet function has a default argument for name. If you call greet() without arguments, it greets "Guest." If you provide a name, it greets that name instead.
You define default arguments in the function signature. The syntax is straightforward: you assign a value to the parameter in the function definition.
Notice how b has a default value of 5. When you call add_numbers(3), it adds 3 and the default value of 5. You can also override this default by providing a second argument.
It’s essential to place parameters with default values after those without them. Otherwise, you'll run into a SyntaxError.
You can specify multiple default arguments in a function. Just remember to follow the same rules as before—non-default arguments should come first.
Here's an example with multiple defaults:
In this function, animal_type has a default value of "dog." If you call describe_pet("Max"), it will use "dog" for animal_type. If you provide both arguments, it will use the values you supplied.
While default arguments are incredibly useful, they can lead to unexpected behavior, especially when mutable types are involved. Let’s explore this further.
If you use a mutable object, like a list or a dictionary, as a default argument, you'll encounter unexpected behavior due to Python's handling of mutable objects. When the function is called, the mutable object is created once and shared across all calls.
Consider this example:
In this case, every time you call append_to_list, it modifies the same list. This can lead to confusion and bugs if you're not careful.
Avoid using mutable types as default arguments. Instead, use None and initialize inside the function:
By initializing the list within the function, each call gets a fresh list.
Understanding default arguments can help you write cleaner and more maintainable code. Here are a few scenarios where default arguments shine:
You might have a function that configures settings for a service. Default arguments can make it easy to set common configurations while allowing for customization.
When building API client libraries, default arguments can help streamline function calls while allowing for flexibility.
In both cases, default arguments provide a balance between usability and flexibility.
As you incorporate default arguments in your functions, keep these best practices in mind:
Now that you understand how to effectively use default arguments in Python, you are ready to explore the more advanced concepts of *args and **kwargs. These features will allow you to write even more flexible functions that can accept varying numbers of arguments.
In the next chapter, we will delve into how to leverage these powerful features to enhance your function definitions and calls.