Looping statements repeat a block of code multiple times. They are one of the most powerful tools in programming — without loops, you'd have to write the same code 100 times to do 100 things.
for LoopUsed to iterate over a sequence — going through items one by one. Most commonly used when you know how many times to repeat.
for variable in sequence: code block
From your file (control-flow.py):
# Iterating over a string — one character at a time for char in "Hello": print(char) # Output: H e l l o (each on its own line)
range()range() generates a sequence of numbers. It has three forms:
| Form | Meaning | Example | Generates |
|---|---|---|---|
range(n) | 0 up to n-1 | range(5) | 0, 1, 2, 3, 4 |
range(start, stop) | start up to stop-1 | range(2, 6) | 2, 3, 4, 5 |
range(start, stop, step) | start to stop-1, by step | range(10, 0, -1) | 10, 9, 8 … 1 |
# Count down from 10 to 1 (from file) for num in range(10, 0, -1): print(num)
for loopsA loop inside a loop. From the star pattern example:
# Print a 5×5 star grid for _ in range(5): # outer: 5 rows for _ in range(5): # inner: 5 stars per row print("*", end=" ") print() # newline after each row
_ is used as a variable when you don't actually need the loop variable. It's a convention that says "I'm not using this value."
while LoopRepeats a block of code as long as a condition stays True. Use when you don't know in advance how many iterations you need.
initialization while condition: code block updation # important! avoid infinite loop
while loop, the condition never becomes False and the loop runs forever (infinite loop). Always make sure the condition will eventually be False.
From file:
# Print from 11 down, by 2 (odd numbers descending) num = 11 while num > 0: print(num) num -= 2 # updation — num decreases each time
number = 1234 reverse = 0 while number > 0: remainder = number % 10 # get last digit reverse = reverse * 10 + remainder number = number // 10 # remove last digit print(reverse) # 4321 # How it works step by step: # number=1234 → remainder=4, reverse=4, number=123 # number=123 → remainder=3, reverse=43, number=12 # number=12 → remainder=2, reverse=432, number=1 # number=1 → remainder=1, reverse=4321, number=0 → stop
else with LoopsPython allows an else after a loop. The else block runs only if the loop finishes normally (not interrupted by break).
for i in range(3): print(i) else: print("Loop finished normally") # Output: 0 1 2 Loop finished normally
Used to change the normal flow of a loop. Your teacher covers three: break, continue, and pass.
break — Exit the loop immediatelyfor i in range(5): if i == 3: break print(i) else: print("completed the loop") # Output: 0 1 2 # else does NOT run — loop was broken
continue — Skip this iteration, keep goingfor i in range(5): if i == 3: continue # skip i=3 print(i) else: print("completed the loop") # Output: 0 1 2 4 (3 is skipped) # else DOES run — loop completed normally
pass — Placeholder, do nothingfor i in range(5): if i == 3: pass # does nothing — loop continues normally print(i) else: print("completed the loop") # Output: 0 1 2 3 4 completed the loop # All numbers print including 3 — pass didn't skip anything
| Statement | Effect | else runs? |
|---|---|---|
break | Exits loop immediately | No |
continue | Skips current iteration only | Yes (loop finishes) |
pass | Does nothing — placeholder | Yes |
for loop.5 x 1 = 5
n = int(input("Enter a number: ")) for i in range(1, 11): print(f"{n} x {i} = {n * i}")
while loop to calculate the sum of numbers from 1 to 100.
i = 1 total = 0 while i <= 100: total += i i += 1 print("Sum:", total) # 5050
* * * * * * * * * * * * * * *
for row in range(1, 6): for col in range(row): print("*", end=" ") print()
continue to skip all multiples of 3.
for i in range(1, 16): if i % 3 == 0: continue print(i) # Output: 1 2 4 5 7 8 10 11 13 14
while loop.
number = int(input("Enter a number: ")) reverse = 0 while number > 0: remainder = number % 10 reverse = reverse * 10 + remainder number = number // 10 print("Reversed:", reverse)
range(5) generate?range(10, 0, -1) generates which sequence?break do to the loop's else block?for i in range(5): if i==3: continue \n print(i) — what prints?for i in range(5): if i==3: pass \n print(i) — what prints?while loop?_ as the loop variable in for _ in range(5)?while over for?