Python loops look short, but a stop value, one level of indentation, or a missing update can change the answer completely. If you understand variables and conditions but still cannot predict a loop's exact output, the cure is a line-by-line dry run. A line-by-line dry run traces every changed value before more complex patterns.
How a loop repeats a block in Python
An iteration is one pass through a loop. An iterable is an object that supplies values, such as a list or string. The loop variable receives one value per pass, and the indented body contains the statements that repeat. Python uses indentation, not braces, to decide what belongs to that body.
total = 0
for value in [3, 5, 8]:
total += value
print(total)Before the loop, total = 0. Reading 3 changes it to 3; reading 5 changes it to 8; reading 8 changes it to 16. The printed lines are therefore:
3
8
16The body runs three times because the list has three values. No index is needed when the values themselves are all you require. The Coding & Skills category is a useful starting point for building this wider programming foundation.
Python for loops over lists, strings, and positions
A for loop can read values directly while an accumulator keeps a running result:
distances = [120, 80, 150]
total_distance = 0
for distance in distances:
total_distance += distance
print(total_distance)The trace is 0 -> 120 -> 200 -> 350, so the final printed distance is 350. A string is iterable too. for letter in "PY": print(letter) prints P and then Y on separate lines.
Ask for positions only when they carry meaning. enumerate() supplies them without a separate counter:
tasks = ["read", "code", "test"]
for position, task in enumerate(tasks, start=1):
print(position, task)The output is 1 read, 2 code, and 3 test, each on its own line. This is clearer than creating position and remembering to execute position = position + 1 on every pass.
range() with start, stop, and step
Read range(start, stop, step) as an integer progression whose stop value is excluded.
Expression | Trace | Result |
|---|---|---|
|
|
|
|
|
|
For sum(range(2, 11, 2)), the generated values are 2, 4, 6, 8, 10. Thus 2 + 4 + 6 + 8 + 10 = 30. Notice the boundary rule: range(1, 5) produces 1, 2, 3, 4; use range(1, 6) when 5 must be included.
Python Programming continues from integer ranges into functions, data structures, and small programs.
while loops for condition-driven repetition
Use while when repetition depends on a changing condition rather than values already present in an iterable. Python checks the condition before every pass, so a false starting condition produces zero passes.
balance = 120
months = 0
while balance < 300:
balance += 45
months += 1
print(months, balance)The four printed lines are 1 165, 2 210, 3 255, and 4 300. Python then checks balance < 300; it is false, so the loop exits. Starting with balance = 320 would skip the body entirely. Here, balance += 45 is the essential update that moves the state towards termination.

break, continue, and the loop else clause
continue skips the rest of the current pass. break exits the current loop.
readings = [18, -1, 23, 31, 27]
for reading in readings:
if reading == -1:
continue
if reading > 30:
print("alert", reading)
break
print("accepted", reading)The output, in order, is accepted 18, accepted 23, and alert 31. The sentinel -1 is skipped, 31 ends the loop, and 27 is never visited.
A loop's else block runs only if the loop finishes without break. To search targets = [4, 7, 9] for 8, compare all three values and then print not found. When searching the same list for 7, enumerate() reports index 1; executing break means not found is not printed.
Nested loops and the number of iterations
In a nested loop, the complete inner loop normally runs once for each outer pass.
for row in range(1, 3):
for column in range(1, 4):
print(row * column, end=" ")
print()The output rows are 1 2 3 and 2 4 6. There are 2 outer iterations and 3 inner iterations per row, so the inner body executes 2 * 3 = 6 times. A break in the inner loop exits only that inner loop.

Repeated traversal becomes important in later applications such as Graph MCQs: 10 Solved BFS, DFS, Connectivity (GATE) and Binary Tree MCQs: 11 Solved BST, AVL, Heaps (GATE).
Common loop mistakes and three check-yourself exercises
Three mistakes deserve a deliberate dry run:
Off by one:
range(1, 5)stops at4, whilerange(1, 6)includes5.Missing update:
count = 3; while count > 0: print(count)prints3forever becausecountnever changes. Addcount -= 1to print3,2,1, then stop.Modifying a list during iteration: removing values below
4from[1, 2, 3, 4]in the same loop can leave[2, 4]because each removal shifts the next item. Building[n for n in numbers if n >= 4]safely gives[4].
Solve these exercises, then compare your work with the solutions:
Sum the squares from
1through4. Check value:1 + 4 + 9 + 16 = 30.Count the vowels in
"education". Check value: the vowels aree, u, a, i, o, so the count is5.Find the first multiple of
7inrange(20, 40)and stop. Check value:21.
Compact solutions
square_total = 0
for number in range(1, 5):
square_total += number * number
print(square_total)
vowel_count = 0
for letter in "education":
if letter in "aeiou":
vowel_count += 1
print(vowel_count)
for number in range(20, 40):
if number % 7 == 0:
first_multiple = number
break
print(first_multiple)The square total moves through 0 -> 1 -> 5 -> 14 -> 30. The vowel count increases once for each of e, u, a, i, o, reaching 5. In the last loop, 20 fails the divisibility test, 21 passes, and break preserves 21 as the first match. All three results match the check values. For more practice, the practice bank has about 30 questions in Python's Control Flow & Looping area.
The short version and the next practice step
Use
forfor values from an iterable.Use
whilefor a condition-driven process.Use
range()for an integer progression.Use
continueto skip one pass.Use
breakto exit the current loop.
The central debugging habit is to record the loop variable and every changed value after each pass. Even the small trace total: 0 -> 3 -> 8 -> 16 exposes what the code is doing.
When you are ready to apply loops inside arrays, searching, and traversal, the DSA Using Python listing is the relevant next path. Run each example, change one input such as a range() step or a while target, and predict the output before executing it.




