Technical skill practice

Python for Loop: Process Items, Break and Use Loop Else

A Python for loop takes items from an iterable and runs an indented body for each one. break ends that loop early. A loop’s else block runs when the loop finishes without break; it does not belong to the last if statement inside the body.

Start with a small, explicit task

Inspect a sequence of fictional package labels until the first damaged package. Return the labels inspected before the stop. Repeat with no damage and with an empty queue. The task is about control flow, so there are no file reads, services or random results to distract from the trace.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_for_loop.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

(['sealed', 'damaged'], 'stopped')

(['sealed', 'sealed'], 'complete')

([], 'complete')

def inspect_packages(labels):
    visited = []
    for label in labels:
        visited.append(label)
        if label == "damaged":
            outcome = "stopped"
            break
    else:
        outcome = "complete"
    return visited, outcome
stopped = inspect_packages(["sealed", "damaged", "sealed"])
complete = inspect_packages(["sealed", "sealed"])
empty = inspect_packages([])
assert stopped == (["sealed", "damaged"], "stopped")
assert complete == (["sealed", "sealed"], "complete")
assert empty == ([], "complete")
print(stopped)
print(complete)
print(empty)

Explain the result

The damaged label is included in visited because it is appended before the condition is tested. The following sealed label is never visited: break transfers control beyond the loop and skips its else block. Moving the append below the check would produce a different trace, so statement order is part of the behavior.

In the complete case, every label is visited and no break occurs. The empty input also reaches else because nothing causes an early break. The name complete is the rule chosen for this exercise; an actual workflow might treat an empty inspection set differently. Define that business rule rather than deriving it from Python syntax.

Avoid these mistakes

Indent else at the level of for when it is a loop else. Be cautious about adding or removing items from the same container being traversed: the resulting visits may no longer match your intended queue. Use a separate output collection when that makes the operation clearer.

Questions, answered

Does for need an index variable?

No. Here label is each actual string. Use an index only when the task requires one.

Will loop else run for an empty list?

Yes in this example: the loop exhausts without break, so its else block executes.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.