Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsfor 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.
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.
#1 Best Overall
| 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.
Rank #2
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.
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.
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.
Best Value
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
forloop 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 onforstatements 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:
Quick Recap
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.




