Defining functions in Python
Function signatures: positional, keyword, and default arguments.
def brew(drink, size="medium", to_go=False):
order = f"{size} {drink}"
if to_go:
order += " to go"
return order
morning = brew("coffee")
afternoon = brew("tea", size="large")
evening = brew("cocoa", to_go=True)
explicit = brew(drink="matcha", size="small", to_go=False)
How it works
- Defaults make
sizeandto_gooptional. - Callers can pass positionally or by keyword.
- Keyword calls read clearly and skip defaults selectively.
Keywords and builtins used here
brewdefifreturn
The run, in numbers
- Lines
- 11
- Characters to type
- 271
- Tokens
- 92
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 61 seconds.
Step 1 of 6 in Functions, step 33 of 72 in Language basics.