Free tools Windows power users keep installed
One-click scans. No signup required.
A Python for loop does not inherently count. It takes items from an iterable one at a time; range() is simply one way to provide a sequence of integers. Once that distinction is clear, the loop syntax—and the reason range(5) ends at 4—makes sense.
What a Python for loop actually does
A for statement asks an iterable for its next item, assigns that item to the loop target, and runs the indented body. It repeats until there are no more items. The iterable determines what the loop receives; it might be a list, a string, or a range of numbers. The Python tutorial describes for as iterating over sequence items in their order, rather than being limited to an arithmetic counter loop: Python tutorial: More Control Flow Tools.
As an Amazon Associate I earn from qualifying purchases.
for word in words:
print(word)
Here, word is assigned each successive item yielded by words. If the program needs the words themselves, there is no need to create numeric indices with range().
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsWhy range(5) stops at 4
range(5) represents the integers starting at zero and stopping before five. Iterating over it produces 0, 1, 2, 3, 4. The stop value marks the boundary; it is excluded.
#1 Best Overall
for i in range(5):
print(i)
This gives five iterations, not four: the values are zero through four. More generally, range(stop) starts at zero, while range(start, stop, step) lets you specify where to start and how much to change the value each time.
for number in range(2, 10, 2):
print(number)
This produces 2, 4, 6, 8. The same rule works with a negative step when the start, stop, and direction agree—for example, range(5, 0, -1) yields 5, 4, 3, 2, 1. The stop endpoint remains excluded. See the Python tutorial’s explanation of the range() function.
Rank #2
A range is not a list
A range object describes an immutable arithmetic sequence; it is not a prebuilt list containing every value. When a loop iterates over it, the range supplies successive values. If a list is specifically needed, convert it explicitly:
PC 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 & 11Outdated 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 matchnumbers = list(range(5))
print(numbers) # [0, 1, 2, 3, 4]
That distinction matters when reading code: range(5) is a way to provide values for iteration, while list(range(5)) creates a list of those values.
Choose the loop form based on what the body needs
| What the loop needs | Useful form | Example |
|---|---|---|
| Each value in a sequence | Direct iteration | for item in items: |
| Both each position and its value | enumerate() |
for index, item in enumerate(items): |
| An integer progression, or indices alone | range(); use range(len(items)) when the indices are needed |
for index in range(len(items)): |
For example, when both the position and value matter, enumerate() supplies them together:
for index, item in enumerate(items):
print(index, item)
The Python tutorial presents enumerate() as a looping technique for obtaining an item’s position along with the item: Python tutorial: Looping Techniques. range(len(items)) is still useful when the index itself is what the loop body needs; the choice depends on the information being used, not on a rule that one form is always wrong.
Two loop-variable details that prevent surprises
Changing the target does not change the next item
The loop target receives the next item supplied by the iterator on each pass. Assigning a different value to that variable inside the body does not tell the iterator what to supply next. The next iteration continues from the iterable’s own sequence. The Python language reference for the for statement describes how each successive item is assigned to the target.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Changing a collection during iteration can be tricky
Removing or adding items to a collection while looping over it can affect which items are processed. The Python tutorial demonstrates iterating over a copy or building a new collection instead of changing the collection being traversed: Python tutorial: More Control Flow Tools.
Quick Recap
Best Value
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.




