Control structures in Python organize how code runs, and the for loop with range is a core tool for repeating actions over predictable sequences. This part of the series shows how range drives clean, compact counting behavior in everyday scripts.
Use the structured reference below to compare range behaviors at a glance and decide which parameters best fit your iteration needs.
| Call | Generated Values | Length | Use Case |
|---|---|---|---|
| range(5) | 0, 1, 2, 3, 4 | 5 | Simple forward counting from zero |
| range(2, 6) | 2, 3, 4, 5 | 4 | Forward counting with custom start and stop |
| range(1, 10, 2) | 1, 3, 5, 7, 9 | 5 | Forward counting with step, skipping values |
| range(5, 0, -1) | 5, 4, 3, 2, 1 | 5 | Reverse counting with negative step |
Using for loop with range for index-based tasks
The for loop with range is ideal when you need an index to access items by position. You explicitly control start, stop, and step while safely iterating over a numeric sequence.
Controlling direction and step with negative range values
Setting a negative step allows reverse iteration, but stop is exclusive, so you must set stop below start to include the desired boundary value. Understanding this boundary behavior prevents empty loops and off-by-one mistakes.
Avoiding off-by-one errors in loop boundaries
Always remember that range stops before the stop value, so a loop meant to include the last index must use stop equal to the length of the target data. Aligning ranges with list lengths keeps logic predictable and safe.
Combining enumerate and range when you need both index and item
If you want the index for control flow and the item for computation, enumerate is often simpler and more readable than pairing range with manual indexing. Reserve manual index access for cases where you must mutate the list during iteration.
Practices for reliable Python iteration with for loop and range
- Prefer enumerate over manual indexing when you need both index and element.
- Align stop with len(sequence) to include the last valid index.
- Use negative step values intentionally and verify start is greater than stop.
- Keep loop bodies side-effect light to avoid fragile iteration behavior.
FAQ
Reader questions
Does range include the stop value in Python loops?
No, range excludes the stop value, so the generated sequence ends one step before the stop you specify.
Can I use range with non-integer start, stop, or step values?
No, range only accepts integers; using floats will raise a TypeError, and you must use alternatives like numpy.arange for fractional steps.
How does a negative step affect the behavior of for loop with range?
A negative step counts downward, and you must set start above stop so the sequence is non-empty and the loop executes.
Will changing the list inside a for loop with range affect the iteration count?
Modifying the list items is safe, but changing the list length while iterating with range can cause skipped elements or index errors, so avoid structural changes during the loop.