Green Hat – Logic & Loops
intermediate25 XP

Loops – For & While

Repeat actions automatically with for loops and while loops

Loops

For Loop

Python
for i in range(1, 6):
    print(i)  # 1 2 3 4 5

for fruit in ["apple","banana","mango"]:
    print(f"I like {fruit}!")

While Loop

Python
lives = 3
while lives > 0:
    print(f"You have {lives} lives")
    lives -= 1
print("Game Over!")

break & continue

Python
for n in range(1, 11):
    if n == 5:
        break      # stop completely
    print(n)
⌨️

Type it yourself

Don't just read it — type the example from above into the box, press Run ▶, and watch what happens. Typing it yourself is how it really sticks! Stuck? Tap Show example.

Python · Playground
Output
Press ▶ Run to see your code come to life…

Quick check

01/2

How many times does 'for i in range(3):' loop?