DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Understand Python for Loops by Seeing What `range()` Actually Does

Python’s for loop iterates over items; range() supplies integers when a numeric progression or index is what you need.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

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().

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

Why 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.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
numbers = 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.

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

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.

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

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.