Loops
๐ Loops
for and while loops, range(), loop control with break/continue, a genuinely unusual Python feature (the loop else clause), and enumerate().
๐ for Loops: Iterating Over a Sequence
Python's for iterates directly over items in a sequence โ not an index counter, unlike a C-style for:
for char in "abc": print(char) # a # b # c
Looping: Go vs Kotlin vs Python
Go uses a single for keyword for every loop shape (counting, condition-based, infinite) โ there's no separate while at all. Kotlin's for (item in collection) is directly comparable to Python's item-based for. Python keeps for (iterate over items) and while (loop on a condition) as two distinct keywords.
๐ข range(): Looping a Specific Number of Times
for i in range(5): # 0, 1, 2, 3, 4 โ stop is EXCLUSIVE print(i) for i in range(1, 5): # 1, 2, 3, 4 โ start, stop print(i) for i in range(0, 10, 2): # 0, 2, 4, 6, 8 โ start, stop, step print(i)
range(n)'s stop value is always exclusive โ range(5) produces 0, 1, 2, 3, 4, five numbers total, never 5 itself. This trips up nearly everyone the first time they need exactly n iterations and reach for range(n) expecting it to count up to n inclusive.
๐ while Loops
count = 0 while count < 3: print(count) count += 1 # 0 # 1 # 2
Python has no do-while construct at all โ the condition is always checked before the loop body runs, at least once via while or not at all.
โญ๏ธ break and continue
for n in range(10): if n == 5: break # stop the loop entirely if n % 2 == 0: continue # skip to the next iteration print(n) # 1, 3
๐ฏ The Loop else Clause
A genuinely distinctive Python feature: for and while loops can have an else clause that runs only if the loop finishes without hitting a break โ useful for search patterns:
numbers = [2, 4, 6, 8] for n in numbers: if n % 2 != 0: print("found an odd number") break else: print("all numbers were even") # this runs โ no break occurred
Neither Go nor Kotlin has anything like this โ it's a Python-specific idiom worth recognizing when reading other people's code, even if you don't reach for it constantly yourself.
๐ข enumerate(): Getting the Index While Iterating
for i, char in enumerate("abc"): print(i, char) # 0 a # 1 b # 2 c
enumerate() pairs each item with its index automatically โ no separate counter variable to manually increment, the way a C-style loop would need.
for
Iterates directly over items in a sequence โ strings, lists, range(), and more.
while
Loops on a condition, checked before each iteration; no do-while equivalent exists.
break / continue
Exit the loop entirely, or skip straight to the next iteration.
enumerate()
Pairs each item with its index โ no manual counter variable needed.
๐ป Coding Challenges
Challenge 1: Sum of Even Numbers
Using range() and a for loop, calculate and print the sum of all even numbers from 1 to 20 (inclusive).
Goal: Practice combining range(), the modulo operator, and loop accumulation.
Challenge 2: Search With Loop else
Given a list of names, write a for loop with a matching else clause that prints "found!" and breaks if a target name is in the list, or prints "not found" via the else clause if it never is.
Goal: Get hands-on with the loop else clause in the exact search scenario it's designed for.
Challenge 3: Numbered List Output
Given a list of three favorite foods, use enumerate() to print each one with a 1-based number in front of it (e.g. "1. Pizza"), not the default 0-based index.
Goal: Practice using enumerate() and adjusting its default starting index for a human-friendly numbered list.
๐ฏ What's Next
Next chapter: Strings & String Formatting โ slicing, string methods, f-strings, and .format().