Control Flow¶
if / elif / else¶
elifandelseare optional.- There is no limit to the number of
elifbranches. - Braces
{ }are always required, even for single-statement blocks.
while¶
Repeats a block as long as the condition is true:
for — range loop¶
Iterates from start to end inclusive, incrementing by 1:
for — for-each loop¶
Iterates over the elements of a list, the characters of a string, or the keys of a dictionary:
# List
fruits = ["apple", "banana", "cherry"]
for fruit in fruits {
write(fruit)
}
# String (character by character)
for ch in "hello" {
write(ch)
}
# Dictionary (iterates over keys)
for key in {"a": 1, "b": 2} {
write(key)
}
break¶
Exits the innermost loop immediately:
continue¶
Skips the rest of the current iteration and moves to the next:
pass¶
A no-op placeholder for empty blocks. Useful when a block is syntactically required but you have nothing to put in it yet: