typestar

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

  1. Defaults make size and to_go optional.
  2. Callers can pass positionally or by keyword.
  3. Keyword calls read clearly and skip defaults selectively.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 6 in Functions, step 33 of 72 in Language basics.

← Previous Next →