Loops

Python Fundamentals — Loops
Python Fundamentals
Course 1 · Chapter 4 · Loops

🔁 Loops

Chapter 3 covered branching. This chapter covers repetition — 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(5) Stops at 4, Not 5

range(n)'s stop value is always exclusiverange(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.

→ Solution

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.

→ Solution

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.

→ Solution

🎯 What's Next

Next chapter: Strings & String Formatting — slicing, string methods, f-strings, and .format().