Default arguments
Not met yet
A parameter can have a default: def label(item, copies=1):. When a call leaves that argument out, the function uses the default; when the call passes one, the default is ignored. Parameters with defaults go after the ones without.
Try it
main.pydef label(item, copies=1):
return f"{item} x{copies}"
print(label("kettle"))
print(label("clock", 3))
Output
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks a parameter with a default must still be passed in every call.
- Thinks a left-out argument becomes None rather than the default in the def line.
- Thinks the default is used even when the call passes a value.
Where it comes up
- Password Generator
- Word Counter and Text Analyser
- Testing the Bank
- Sorting Visualiser
- Summary Statistics Report
- Statistics Toolkit
- Calculus for ML
- Data Story
- Diabetes Risk Classifier
- Iris and Penguins Classifier
- Loan Approval Tree
- Seeing Forecaster
- A/B Test Bandit
- Gridworld Simulator
- Gridworld Solver
- Value Estimator