Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use a Python for loop to process each value from an iterable, such as a list or string. Use a while loop to repeat work as long as a condition is true. range() supplies integer sequences; break exits a loop, continue skips to its next iteration, and a loop’s else runs when it finishes without a break.

How a Python for loop works

A for loop takes values one at a time from an iterable, in the order that iterable provides them. An iterable can be a list, a string, or another object that supplies items; the loop is not inherently a numeric counter.

names = ["Ada", "Linus", "Grace"]
for name in names:
    print(name)

Here, names is the source of values. The indented statements form the loop body, which runs once for each name.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Using range() for integer progressions

range() is useful when a loop needs a sequence of integers. Its stop value is excluded, so range(2, 8, 2) produces 2, 4, and 6—not 8.

for number in range(2, 8, 2):
    print(number)  # 2, 4, 6

range(stop) starts at zero. You can also specify a start and a step, including a negative step to count downward. If you need a sequence’s index, range(len(sequence)) can provide one; when the index is not needed, iterating over the values directly is usually clearer.

When to choose for versus while

Loop How it gets its next iteration Typical stopping rule
for Gets successive values from an iterable. Stops when the iterable is exhausted, unless control flow exits it earlier.
while Checks a Boolean condition. Stops when the condition becomes false, unless control flow exits it earlier.

Choose for for a task such as “do this for each item.” Choose while for “keep doing this until a condition changes.” A while condition is checked before each iteration, so if it is false at the start, the body runs zero times.

How a while loop stops

A while loop repeats while its condition is true. The loop body should update state or otherwise make the condition able to become false; otherwise, the loop can continue indefinitely.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
attempts = 0
while attempts < 3:
    print("Try", attempts + 1)
    attempts += 1

The condition allows three iterations. Each pass increases attempts; after it reaches 3, the condition is false and the loop ends.

How break and continue change a loop

  • break exits the nearest enclosing loop immediately, even if the iterable has more values.
  • continue skips the rest of the current iteration and proceeds to the next iteration of that loop.
for item in items:
    if should_skip(item):
        continue
    if is_match(item):
        print(item)
        break

In this example, skipped items do not reach the match check. Once a matching item is printed, break ends the loop. In nested loops, both statements affect only the nearest enclosing loop—the innermost one—not every loop around it.

What a loop’s else clause means

A loop’s else clause runs if the loop finishes normally without executing break. With a for loop, that means the iterable was exhausted; with a while loop, it means the condition became false. The else does not run if the loop ends through break, return, or an exception.

for item in items:
    if item == target:
        print("Found")
        break
else:
    print("Not found")

Indent the else at the same level as for: it belongs to the loop, not the inner if. This pattern reports a failed search only after the loop has checked all items without finding the target.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Editing a collection during iteration

Adding or removing elements from the same collection a loop is traversing can produce confusing results, because the collection changes while the loop is using it. The Python tutorial advises iterating over a copy or building a new collection instead. For example, iterate over a shallow copy when changes to the original are needed during traversal:

for item in items.copy():
    if should_remove(item):
        items.remove(item)

For transformations or filtering, creating a separate result collection is another way to avoid changing the sequence being traversed.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Official Python documentation

The Python Software Foundation’s Python 3.14.8 tutorial on control flow explains iteration, range(), loop control, and loop else. The Python 3.14.8 language reference for compound statements specifies the statement semantics. Documentation version noted here: Python 3.14.8.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.