Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Control flow and functions

if / elif / else

def grade(score: int) -> str:
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    else:
        return "C"

Loops

for iterates over any iterable (list, string, range, …):

for i in range(3):
    print(i)

items = ["a", "b", "c"]
for item in items:
    print(item)

while repeats while a condition is true.

Functions

Define with def, document with a docstring, optionally annotate parameters and return type:

def add(a: int, b: int) -> int:
    """Return the sum of a and b."""
    return a + b

Multiple return values use a tuple (often unpacked):

def divmod_custom(a: int, b: int) -> tuple[int, int]:
    return a // b, a % b

q, r = divmod_custom(10, 3)

Implement

  1. Write clamp(value: int, low: int, high: int) -> int that returns value limited to [low, high].
  2. Write a loop that prints squares for i from 0 through 5 (e.g. i * i).
  3. Commit and push.