Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

For i in range() in Python: What It Means and How It Works

A Python loop using range() assigns each number in a sequence to its loop variable. Learn its syntax, excluded stop value, steps, and alternatives.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

for i in range(5) repeats the loop body five times, assigning i the values 0 through 4. In Python, range() represents a sequence of integers, and a for loop takes each value in turn. The stop value is excluded, so range(5) does not include 5.

How for i in range() works

Consider this loop:

for i in range(5):
    print(i)

range(5) represents 0, 1, 2, 3, 4. The for statement obtains values from that sequence one at a time, assigns each value to i, and runs the indented body. This prints five lines, ending with 4. The Python tutorial explains that the endpoint is not part of the generated sequence.

As an Amazon Associate I earn from qualifying purchases.

What does i mean?

i is an ordinary variable name, commonly used for an index or counter; Python gives it no special behavior in this loop. Use a name such as count or index if it makes the code’s purpose clearer. The loop target can be any valid assignment target, though a simple name is easiest to follow in a basic example.

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

The three forms of range()

range() accepts a stop value, or a start and stop, with an optional step. In every form, the stop value is excluded.

Form Sequence What it means
range(stop) range(5) → 0, 1, 2, 3, 4 Starts at 0 and advances by 1.
range(start, stop) range(2, 6) → 2, 3, 4, 5 Starts at start and advances by 1.
range(start, stop, step) range(0, 10, 3) → 0, 3, 6, 9 Starts at start and advances by step.

Counting downward

A negative step counts downward when the starting value is greater than the stop value:

for i in range(5, 0, -1):
    print(i)

This supplies 5, 4, 3, 2, 1. The stop remains excluded, so the loop does not print 0.

When a range is empty

A range can contain no values if its first candidate does not meet the stop condition. For example, range(5, 0) is empty: its default step is positive, so it advances away from the smaller stop value. A step of zero is invalid and raises ValueError.

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

What a range object is

range() returns an immutable sequence object, not a list containing every number in advance. The Python documentation for range objects notes that a range stores its start, stop, and step and calculates values as needed; its memory use stays small regardless of how many values it represents.

Convert it to a list only if you need an actual list—for example, to display all the values together or pass them to an API that requires a list:

numbers = list(range(5))
print(numbers)  # [0, 1, 2, 3, 4]

What happens to the loop variable

The loop assigns the next value from the iterator to its target on each iteration. Changing i inside the body does not change what value the range supplies next:

for i in range(3):
    i = 99
    print(i)

This prints 99 three times. At the start of each iteration, the loop assigns the next range value to i, then the body changes it to 99.

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.

After a nonempty loop finishes, the target name remains assigned to the last value supplied by the iterator. If the range is empty, the body does not run, so the loop does not assign that name.

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

When to use range(), direct iteration, or enumerate()

  • Use range() when you need a sequence of numbers—for example, to repeat an action a fixed number of times or work with positions by index.
  • Iterate directly over a collection when you need its values. A for loop can take each list item without using an index.
  • Use enumerate() when you need both an item and its position. The Python tutorial’s section on for statements covers this pattern.

For example, direct iteration is concise when only the values matter:

for name in names:
    print(name)

When both position and value are useful, enumerate() provides both:

for index, name in enumerate(names):
    print(index, name)

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.

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
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.