Conditions and loops

if / elif / else, for over a range or a collection, while, break and continue.

Runs in Zerethon IDE · Python 3.12 Official Python documentation ↗
main.py
for n in range(1, 6):
    if n % 2 == 0:
        print(n, "even")
    elif n == 5:
        print(n, "last")
    else:
        print(n, "odd")

i = 3
while i > 0:
    i -= 1
    if i == 1:
        continue
    print("i =", i)

for index, fruit in enumerate(["apple", "pear"]):
    print(index, fruit)

Output

1 odd
2 even
3 odd
4 even
5 last
i = 2
i = 0
0 apple
1 pear
  • range(1, 6) stops before 6. range(5) is 0 to 4.
  • Use and, or, not — not &&, ||, !.
  • enumerate() gives the index and the item; zip(a, b) walks two lists together.