Intuition The one core idea
A loop is a machine that runs the same block of code again and again. break, continue, and pass are three tiny commands you drop inside that block to steer the machine: leave for good, skip ahead one step, or do nothing at all.
Before you can steer a loop, you must first see clearly what a loop actually is, what "iteration" means, and what a "block" is — this page builds every one of those pieces from nothing.
The parent note (break, continue, pass — when and why ) throws around words like iteration , body , block , condition , range, %, enumerate, and control flow as if you already own them. Here we earn each one, in an order where every idea rests on the one before it.
Picture a single index card, each holding one order: "print hello", "add 1 to x". Python reads cards top to bottom, one at a time.
print ( "hi" ) # one statement
x = x + 1 # another statement
Common mistake "One statement is always one line?"
Why it feels right: most statements do fit on one line.
Why it's not the whole truth: Python lets a single statement span several lines. Inside brackets ( ), [ ], { } it continues automatically (implicit line continuation); otherwise a trailing backslash \ joins lines explicitly.
total = ( 1 + 2 +
3 + 4 ) # one statement, two lines (inside parentheses)
total = 1 + 2 + \
3 + 4 # one statement, joined by backslash
Fix: "statement" = one complete instruction, not literally one physical line.
Why the topic needs it: break, continue, and pass are themselves statements. pass in particular only makes sense once you know Python demands a statement in certain spots.
A block is a group of statements that belong together, marked by being indented (pushed right) under a line ending in a colon :. The block is also called the body .
Figure 1 (above): a for header ending in a colon, then three indented lines wrapped by a yellow bracket labelled "the block (body)", then a final line dedented back to the left showing the block has ended. The yellow bracket groups the indented lines; everything at the same indentation belongs to the same block. When indentation goes back to the left, the block has ended.
Common mistake "Empty blocks are allowed, right?"
Why it feels right: in some languages you can leave { } empty.
Why it's wrong: Python requires at least one statement inside every block. An empty block is a syntax error.
Fix: that is exactly the job of pass — a legal statement that does nothing, so the block isn't empty.
Why the topic needs it: the "body" the parent talks about is this block. break/continue skip parts of it; pass fills it.
Definition Condition (Boolean expression)
A condition is an expression that evaluates to either True or False . It is a yes/no question the computer can answer.
<= symbol
<= means less than or equal to . So i * i <= n reads "is i squared less than or equal to n?" — True while i squared has not yet passed n. Its cousins are < (less than), > (greater than), >= (greater than or equal to).
Examples of questions and their answers:
Expression
Reads as
Result
x == 2
"is x equal to 2?"
True / False
n % 3 == 0
"does n divide evenly by 3?"
True / False
i * i <= n
"is i squared less than or equal to n?"
True / False
= versus ==
Why it confuses: they look almost the same.
Why it matters: = assigns (puts a value into a name). == asks (compares two values, gives True/False).
Fix: single = = "make it so"; double == = "is it so?"
Why the topic needs it: every break/continue in the parent lives inside an if <condition>: — the keyword only fires when the test is True. See Conditionals (if-elif-else) .
Picture sharing a sweets among b kids as evenly as possible; % is how many sweets are left in your hand.
%
7 % 3 → 7 = 3+3+1, remainder 1 .
9 % 3 → 9 = 3+3+3, remainder 0 .
10 % 2 → remainder 0 (even).
11 % 2 → remainder 1 (odd).
The magic trick: n % k == 0 means "k divides n evenly" . That is exactly how the parent detects multiples of 3 and how Prime Number Checking tests for divisors.
Figure 2 (above): three rounded boxes joined by arrows forming a cycle — "take next item / check condition" (blue) at the top, an arrow down to "run the body" (yellow), a curved "repeat" arrow back up to the top, and a green exit arrow to "no items left → stop". A for loop's cycle is: take the next item → run the body → come back → take the next item... until items run out. A while loop's cycle is: check condition → run body → recheck → ... until the condition is False.
Definition Two loop kinds
for loop (For Loops ): walks through a fixed collection of items, one at a time.
while loop (While Loops ): repeats as long as a condition stays True.
Intuition Infinite loops and why
break matters
A while True: loop's condition is always True, so on its own it would never stop — the program hangs forever. This is deliberate: you write while True: when you don't know in advance how many rounds you need, and you rely on a break inside the body to escape once some event happens.
while True :
answer = input ( "password? " )
if answer == "open" :
break # the ONLY exit — without it, this runs forever
print ( "try again" )
Takeaway: in a while True: loop, break is not optional flavour — it is the whole termination mechanism.
Why the topic needs it: break/continue/pass are loop-steering commands (well, pass is broader, but the parent uses it inside loops too). Without knowing what an iteration is, "skip this iteration" is meaningless.
range
range(a, b) produces the numbers ==from a up to but NOT including b==. range(n) is short for range(0, n), i.e. 0, 1, ..., n-1.
step (third argument)
range(a, b, step) counts from a toward b in jumps of size step instead of 1.
range(0, 10, 2) → 0, 2, 4, 6, 8 (jump by 2 — evens).
range(1, 10, 3) → 1, 4, 7 (jump by 3).
A negative step counts downward : range(5, 0, -1) → 5, 4, 3, 2, 1.
The stop rule is unchanged: b is never included.
Worked example Reading range
range(4) → 0, 1, 2, 3 (four numbers, stops before 4).
range(1, 11) → 1, 2, 3, ..., 10 (stops before 11).
range(0, 10, 2) → 0, 2, 4, 6, 8.
Common mistake Off-by-one: "range(4) gives 1,2,3,4?"
Why it feels right: we count 1–4 in daily life.
Why it's wrong: Python starts at 0 and excludes the top. range(4) = {0,1,2,3}, which is why the parent's for i in range(4) prints 0,1,2,3.
Fix: picture a ruler that starts at 0 and stops just before the last mark.
range(4) is a list, so I can print it directly?"
Why it feels right: it behaves like a sequence of numbers.
Why it's wrong: range returns a lazy range object , not a list. It generates each number on demand (saving memory — even range(10**9) costs almost nothing). print(range(4)) shows range(0, 4), not the numbers.
Fix: wrap it: list(range(4)) → [0, 1, 2, 3] when you actually need the concrete list. In a for loop you never need to, because the loop just asks for one number at a time.
Why the topic needs it: almost every for example in the parent iterates over a range.
enumerate
enumerate(S) walks a collection S and hands you two things each step: the position number (index, starting at 0) and the item itself.
for i, n in enumerate ([ "Asha" , "Ravi" ]):
print (i, n) # 0 Asha / 1 Ravi
Picture a coat-check: each coat (item n) comes with a numbered tag (index i).
start argument
enumerate(S, start=k) makes the counting begin at k instead of 0.
for i, n in enumerate ([ "Asha" , "Ravi" ], start = 1 ):
print (i, n) # 1 Asha / 2 Ravi
Handy when you want human-friendly "1st, 2nd, 3rd" numbering rather than 0-based.
enumerate gives me a list of pairs?"
Why it feels right: it clearly produces (index, item) pairs.
Why it's wrong: like range, enumerate returns a lazy iterable (an enumerate object), producing one pair at a time. print(enumerate(x)) shows an object, not pairs.
Fix: in a for loop it feeds pairs directly; to see them all at once use list(enumerate(x)).
Why the topic needs it: the parent's search example uses enumerate so that when break fires it can report where the match was found.
Control flow is the order in which Python actually runs your statements. Normally it flows straight down; loops, ifs, break, and continue bend that path.
Figure 3 (above): a dotted vertical "loop top" line on the left, three "statement" lines stacked in the middle (the body), and an "AFTER loop" marker on the right. Three coloured arrows leave the body: a blue arrow curving back to the loop top ("normal: next iteration"), a yellow arrow jumping early back to the top ("continue: skip rest"), and a red arrow shooting out to "AFTER loop" ("break: leave loop").
normal (blue): fall straight through to the next line, then next iteration.
continue (yellow): jump early back to the top of the loop for the next item.
break (red): leave the loop completely, land on the first line after it.
break and continue only touch the innermost loop
When one loop sits inside another (a nested loop ), break and continue act on the nearest enclosing loop only — the one they physically sit inside.
for row in range ( 3 ):
for col in range ( 3 ):
if col == 1 :
break # exits ONLY the inner (col) loop
print ( "row done" , row) # the outer loop keeps going: prints 3 times
Takeaway: a single break will not escape all levels at once. To leave an outer loop you need another break (often driven by a flag) or restructuring — a subtlety worth remembering before you steer multi-level loops.
Intuition Why "control flow" is the whole point
Everything the parent teaches is about bending control flow cleanly. break/continue are jumps; pass is the one that doesn't jump (it just fills a required slot). Seeing these three arrows is seeing the entire topic.
else
A loop may have an else: block that runs ==only if the loop ended normally, never hitting break==.
Picture a security guard checking every door: if they get through all doors without an alarm (break), the else says "all clear". If an alarm fired (a break), the "all clear" is skipped. Full detail lives in for...else and while...else .
Common mistake "If the loop never runs, the
else is skipped too?"
Why it feels right: it seems the else should belong to "the loop actually happening".
Why it's wrong: the else runs whenever the loop finished without a break — and a loop that does zero iterations also finished without a break. So the else still runs .
for x in []: # empty — body runs zero times
print (x)
else :
print ( "else ran" ) # this DOES print
Fix: read the rule literally: "no break happened" → else runs, even when there were no iterations at all.
Intuition How to read this diagram
Each box is one foundation from this page. An arrow A --> B means "A feeds into B" — you should understand A before B makes full sense. Follow the arrows and they all flow toward the bottom box, break / continue / pass : that is the topic these foundations exist to support.
Statement one instruction
enumerate index plus item
Control flow the path taken
Loop else finished cleanly
Test yourself — each line is a question ::: answer you should be able to fill instantly.
What is a statement? One complete instruction Python can execute; it may span multiple lines inside brackets or via a backslash.
How can one statement span several lines? Automatically inside ()/[]/{}, or explicitly with a trailing backslash \.
What marks a block in Python? Indentation under a line ending in a colon :.
Why can't a block be empty? Python's syntax requires at least one statement; that's what pass provides.
What does a condition evaluate to? True or False (a yes/no answer).
What does <= mean? Less than or equal to.
Difference between = and ==? = assigns a value; == compares two values and returns True/False.
What does 7 % 3 give and what does it mean? 1 — the remainder after dividing 7 by 3.
What does n % k == 0 tell you? That k divides n evenly (no remainder).
What is one iteration? One full run of the loop's body.
What numbers does range(4) produce? 0, 1, 2, 3 (stops before 4).
What does the third argument of range do? Sets the step (stride); e.g. range(0,10,2) gives 0,2,4,6,8, and a negative step counts down.
Do range and enumerate return lists? No — they return lazy iterables that yield one value at a time; use list(...) to materialize them.
What does enumerate give you each step? Both the index (position) and the item.
How do you make enumerate start counting at 1? enumerate(S, start=1).
What is control flow? The order in which statements are actually executed.
In nested loops, which loop does break exit? Only the innermost (nearest enclosing) loop.
How does while True: avoid running forever? A break inside its body is the only way out.
When does a loop's else run? Whenever the loop finishes without hitting break — including when it ran zero iterations.