Skip to content
UPEEC

Python Control Flow

if/elif/else, for and while loops, and the match statement in Python.

Beginner
Comments 0
for i, value in enumerate(["a", "b"]):   # index and value
    print(i, value)

for a, b in zip(list1, list2):            # iterate in parallel
    ...

match command:                            # Python 3.10+
    case "on":  led.on()
    case "off": led.off()
    case _:     print("unknown")

A loop's else block runs only if the loop finished without break — handy for search loops.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.