Use a Python for loop to process values 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 when needed; break exits a loop, continue skips to its next iteration, and a loop’s else runs only when it finishes without break.
What does a Python for loop do?
A for loop takes successive values from an iterable and runs its indented body once for each value, in the iterable’s order. An iterable can be a list, a string, or another object that supplies values. A for loop is therefore not inherently a numeric counter.
As an Amazon Associate I earn from qualifying purchases.
names = ["Ada", "Linus", "Grace"]
for name in names:
print(name)
Here, names supplies each value in turn, name refers to the current value, and the indented print statement runs once per name. The loop normally ends when the iterable has no more values.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesHow does range() work?
range() produces an integer progression, commonly used when a loop needs numbers. Its stop value is exclusive: the sequence ends before that value. You can provide a start and a step as well as a stop.
#1 Best Overall
for number in range(2, 8, 2):
print(number) # 2, 4, 6
This starts at 2, advances by 2, and stops before 8. With one argument, range(stop) starts at zero and excludes stop. A negative step counts downward.
If you need an index, range(len(sequence)) can provide one. When you only need each value, iterating over the items directly is usually clearer. The Python tutorial’s control-flow chapter demonstrates both forms.
Rank #2
When should you use while instead?
A while loop repeats as long as its condition is true. Python checks the condition before each iteration, so if it is false at the first check, the body runs zero times. Use while when repetition depends on a condition changing rather than on exhausting a known iterable.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11attempts = 0
while attempts < 3:
print("Try", attempts + 1)
attempts += 1
The condition is attempts < 3, and the body increments attempts. That update eventually makes the condition false. If a while condition never becomes false, the loop can continue indefinitely; make the exit path or state update clear.
How do you choose between for and while?
| Loop | What controls repetition | Common fit |
|---|---|---|
for |
Successive values supplied by an iterable | Perform an operation for each item, or for each number from range() |
while |
A Boolean condition checked before each iteration | Keep repeating until some state or input changes the condition to false |
Ask whether the task is “do this for each item” or “keep doing this until a condition changes.” That distinction is more useful than thinking of for as counting and while as a special kind of counter.
What do break and continue do?
break exits the nearest enclosing loop immediately, even if the iterable still has values. continue skips the remainder of the current iteration and proceeds to the next cycle of that loop.
for item in items:
if should_skip(item):
continue
if is_match(item):
print(item)
break
In this example, a skipped item does not reach the match check. A matching item is printed, then break ends the loop. In nested loops, either statement affects only the nearest enclosing loop—the innermost one—not every loop around it.
What does a loop’s else mean?
A loop’s else suite runs when the loop completes normally without executing break. For a for loop, that usually means the iterable was exhausted; for a while loop, it means the condition became false. A break skips the suite, and a return or exception that leaves the loop also prevents it from running.
Best Value
for item in items:
if item == target:
print("Found")
break
else:
print("Not found")
Indent else to align with for, not with the loop body. This pattern reports failure only if the search finishes without finding a match. The Python tutorial describes this loop-else behavior.
What common loop mistakes should you avoid?
- Expecting
range()to include its stop: it does not. Choose a stop value that accounts for the exclusive endpoint. - Writing a
whileloop without a stopping path: verify that the body or outside event can make the condition false. - Confusing loop
elsewith anif–else: it is tied to whether the loop ended withoutbreak, not simply to a condition being false inside the body. - Changing a collection while iterating over it: adding or removing items from the same collection during traversal can produce confusing results. Iterate over a copy or build a new collection instead; the Python tutorial discusses this pitfall.
Where can you check the exact syntax?
The Python 3.14.8 tutorial on control-flow tools covers for, range(), loop else, and iterating over collections. For formal statement semantics, consult the Python 3.14.8 language reference on compound statements. Documentation labels and version numbers can change as new Python releases are published.
Quick Recap
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.
Recommended Free Tools




