Skip to main content

3.1 Control Flow Fundamentals

Python control flow uses indentation-based blocks and clear statement forms.

if / elif / else

if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"

for loops

Python for iterates over iterable objects directly.

for word in ["cat", "window", "defenestrate"]:
print(word, len(word))

range()

range() creates an immutable arithmetic progression, commonly used in loops.

for i in range(0, 10, 2):
print(i)

while, break, continue, loop else

  • break exits the nearest loop.
  • continue jumps to next iteration.
  • Loop else runs when no break occurs.

Official references