Default arguments let you give a function a value to use if no value is provided. This makes your functions easier to use and avoids errors.
Default arguments in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
def function_name(parameter1=default_value1, parameter2=default_value2): # function body
Default values are set using the equals sign (=) after the parameter name.
Parameters with default values should come after parameters without defaults.
Examples
Python
def greet(name="friend"): print(f"Hello, {name}!")
Python
def power(base, exponent=2): return base ** exponent
Python
def describe_pet(pet_name, animal_type="dog"): print(f"I have a {animal_type} named {pet_name}.")
Sample Program
This program shows how the greet function works with and without giving a name.
Python
def greet(name="friend"): print(f"Hello, {name}!") greet() greet("Alice")
Important Notes
Default arguments are evaluated once when the function is defined, not each time it is called.
Be careful when using mutable default arguments like lists or dictionaries.
Summary
Default arguments let functions have optional inputs with preset values.
They make functions easier to use and more flexible.
Always put parameters with defaults after those without.
Practice
1. What is the main purpose of default arguments in Python functions?
easy
Solution
Step 1: Understand default arguments
Default arguments allow a function to have parameters with preset values if the caller does not provide them.Step 2: Identify the purpose
This makes the function easier to call without needing all arguments every time.Final Answer:
To provide preset values for parameters if no argument is given -> Option BQuick Check:
Default arguments = preset values [OK]
Hint: Default arguments fill in missing inputs automatically [OK]
Common Mistakes:
- Thinking default arguments speed up code
- Believing default arguments force all inputs
- Confusing default arguments with variable creation
2. Which of the following function definitions uses default arguments correctly?
easy
Solution
Step 1: Check default argument placement
Parameters with default values must come after parameters without defaults.Step 2: Analyze each option
def greet(name='Friend', age): places a default before a non-default parameter, which is invalid syntax. Options C and D have syntax errors. def greet(name, age=30): correctly places the default argument after a required parameter.Final Answer:
def greet(name, age=30): -> Option CQuick Check:
Defaults after required params = correct syntax [OK]
Hint: Put default parameters after required ones [OK]
Common Mistakes:
- Placing default arguments before required ones
- Leaving default values empty
- Using invalid syntax for defaults
3. What is the output of this code?
def multiply(a, b=2):
return a * b
print(multiply(4))
print(multiply(4, 3))medium
Solution
Step 1: Understand default argument usage
When multiply(4) is called, b uses its default value 2, so 4 * 2 = 8.Step 2: Evaluate second call
When multiply(4, 3) is called, b is 3, so 4 * 3 = 12.Final Answer:
8 and 12 -> Option DQuick Check:
Default used first call, explicit second call = 8 and 12 [OK]
Hint: Default used if argument missing, else explicit value [OK]
Common Mistakes:
- Assuming default is ignored when argument is given
- Confusing order of arguments
- Expecting error when default is used
4. Find the error in this function definition:
def add_numbers(x=5, y):
return x + ymedium
Solution
Step 1: Check parameter order
Parameters with default values must come after parameters without defaults in Python.Step 2: Identify error
Here, x has a default but y does not, so this causes a SyntaxError.Final Answer:
Default argument before non-default argument causes SyntaxError -> Option AQuick Check:
Default before required param = SyntaxError [OK]
Hint: Non-default params must come before default ones [OK]
Common Mistakes:
- Ignoring parameter order rules
- Thinking missing return causes error here
- Assuming function name is invalid
5. You want a function that greets a user with their name and an optional greeting word (default is 'Hello'). Which function definition is correct and prints "Hi, Alice!" when called as
greet('Alice', 'Hi') and "Hello, Bob!" when called as greet('Bob')?hard
Solution
Step 1: Check parameter order and defaults
Parameters with defaults must come after those without. def greet(name, greeting='Hello'): print(f"{greeting}, {name}!") has name first (no default), greeting second (default 'Hello').Step 2: Verify behavior
Calling greet('Alice', 'Hi') prints "Hi, Alice!". Calling greet('Bob') uses default greeting "Hello" and prints "Hello, Bob!".Final Answer:
def greet(name, greeting='Hello'): print(f"{greeting}, {name}!") -> Option AQuick Check:
Defaults after required params, works as expected [OK]
Hint: Put required params first, defaults after [OK]
Common Mistakes:
- Placing default param before required param
- Not providing default for optional greeting
- Using default for name causing unexpected calls
