What are Default Arguments?
Default arguments in Python allow function parameters to have a predefined value if no argument is provided during the function call. This makes functions more flexible and reduces the need for multiple function definitions.

Syntax of Default Arguments
def greet(name="Guest"): print("Hello, " + name) greet() # Output: Hello, Guest greet("User") # Output: Hello, User
How Default Arguments Work?
- If an argument is provided, it overrides the default value.
- If no argument is passed, the function uses the default value.
- Default parameters must come after non-default parameters.
Example: Function with Multiple Default Arguments
def order(item, quantity=1, price=50): total = quantity * price print(f"Order: {item}, Quantity: {quantity}, Total Price: {total}") order("Book") # Uses default quantity and price order("Pen", 3) # Uses default price, but changes quantity order("Notebook", 2, 100) # Overrides both defaults
Important Points
- Mutable default arguments (like lists or dictionaries) can lead to unexpected behavior.
- Use immutable types (like strings, numbers, or tuples) as default values to avoid unintended changes.
Example of Mutable Default Argument Issue
def add_item(item, item_list=[]): item_list.append(item) return item_list print(add_item("Apple")) # Output: ['Apple'] print(add_item("Banana")) # Output: ['Apple', 'Banana'] (Unexpected!)
Solution: Use None
as the default and initialize inside the function.
def add_item(item, item_list=None): if item_list is None: item_list = [] item_list.append(item) return item_list
Use Cases of Default Arguments
- Setting default values for function parameters.
- Creating flexible and reusable functions.
- Reducing the number of function overloads.
Common Interview Questions on Default Arguments
1. What are default arguments in Python?
Default arguments allow function parameters to have a predefined value if no argument is provided during the function call.
📌 Companies: TCS, Wipro, Infosys
2. What is the rule for defining default arguments in a function?
Default arguments must always come after non-default arguments.
📌 Companies: Accenture, Capgemini, Cognizant
3. What happens if a mutable object (like a list) is used as a default argument?
The same object is shared across multiple function calls, which may lead to unexpected behavior.
📌 Companies: Amazon, Google, Microsoft
4. How can you avoid issues with mutable default arguments in Python?
Use None
as a default and initialize the mutable object inside the function.
📌 Companies: Oracle, IBM, JP Morgan Chase
5. What is the difference between default arguments and keyword arguments?
Default arguments define preset values for parameters, whereas keyword arguments allow passing values by parameter names during function calls.
📌 Companies: HCL, Cisco, Goldman Sachs
Additional Resources for Default Arguments
1️⃣ Python Official Documentation – Default Argument Values
Conclusion
Default arguments make Python functions more versatile, reducing redundant code while maintaining clarity and efficiency.
Default Arguments
Question
Your answer:
Correct answer:
Your Answers