Default Parameters
22 minutes read
Beginner friendly
Includes practice
Hi there! Today you’ll learn how to give your Python functions built-in backup values—so they still work even when the caller leaves something out.
This feature is called default parameters.
Runtic's Learning Tips
Think of default parameters like polite defaults: your code keeps working even if someone forgets to pass a value.
Reading examples aloud helps you understand the behavior line by line—try explaining what
greet()will print.Even experienced coders use lookup hints for mutable defaults—don’t feel bad revisiting this!
The Big Idea
Sometimes a function has a typical value that should be used unless the caller says otherwise.
Instead of forcing the user to pass this value every time, we can bake it right into the function header.
Real-world analogy
- Think of ordering coffee: if you say “Regular, please,” the barista automatically adds milk and sugar unless you specify a change.
- In Python, you can set
milk='yes', sugar='yes'as defaults.
Basic Syntax
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
- The
=goes in the function header, not in the call. - Parameters with defaults are optional when calling the function.
Worked Example
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Quick Practice
2 minutesTest how well you understand the behavior of functions with default parameters.
What will `greet()` print if you don't pass an argument?
How do you override the default in `greet(name='friend')`?
Parameter Ordering Rule
Rule: Required parameters must come before optional (default) ones.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
A Heads-Up on Mutable Defaults
Watch out when using things like lists or dictionaries as default values.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
⚠ Default list bucket=[] is shared between calls!
✅ Safer Pattern:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Why Mutable Defaults Are Risky
- Default values like lists are created once during function definition
- They're shared across all future calls
- This can lead to unexpected behavior like growing lists across multiple uses
Practice Zone
Try filling in these function blanks below. Use default parameters where appropriate.
Remember: tax and discount should already have fallback values.
Practice Challenge
Write a function called final_price(price,tax_rate=0.05,discount=0.0)that returns the total price after adding tax and subtracting any discount. Then call it with a price of 100 to verify the result
Return the price after adding tax and subtracting discount.
Practice Challenge
Write a function called letter_grade(score,passing=60)that returns "Pass"if the score is greater than or equal to the passing mark, and "Fail"otherwise. Test it with a score above 60 to verify the result.
If score is greater than or equal to passing, return 'Pass'; otherwise return 'Fail'.
Quick Summary
Default parameters allow functions to run with optional inputs
Required parameters must go before defaults
Avoid using mutable values like
[]or{}as defaults
Reflection Prompt
Ask yourself:
- Which parameters did you choose to make optional, and why?
- Did you accidentally place a required parameter after a default one?
Write a quick sentence to clarify your thinking before moving forward.
Mini Self-Quiz
Don't scroll back – see how much you retained:
Quick Practice
3 minutesCheck your understanding of default parameters.
Where must the `=` default assignment appear?
True or False: `def f(x=[])` is safe for lists.
Fill in the blank: Parameters with defaults are _______ when calling the function.
- You learned how to set default values for function parameters
- You practiced defining functions with both required and optional arguments
- You discovered the pitfalls of using mutable default values like lists
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.