Python range
Learn Python's range type: half-open numeric sequences, start/stop/step, negative steps, indexing, slicing, membership, and why ranges do not materialize a list.
Basic
Python's
range type represents an arithmetic progression without first building a list of every value. That makes it a natural fit when the values follow a regular start, stop, and step pattern.
Start, stop, and step
You can call
range with one, two, or three integer arguments. With one argument it starts at 0. With two, you provide the start and stop. With three, the third value is the step. The stop value is never included.
Output
Ready to run.
The one-argument form
range(5) represents 0 through 4. The two-argument form
range(2, 7) represents 2 through 6. The step controls how the next value is calculated.
A range can count backwards
A negative step makes the progression move downward. The same stop-excluded rule still applies, but the start must be positioned so that repeated negative steps can move toward the stop value.
Output
Ready to run.
When the range is empty
Some start, stop, and step combinations describe no values at all. Python does not treat an empty range as an error; it simply represents an empty sequence.
Output
Ready to run.
Range objects are sequences
A range object supports useful sequence operations such as indexing, negative indexing, slicing, and length. These operations work on the sequence represented by the range rather than requiring you to build a list first.
Output
Ready to run.
Membership can follow the pattern
A range also answers membership questions. A value is a member when it fits the represented progression; it is not enough for the value merely to lie between the start and stop boundaries. For a stepped range, the value must land on one of its steps.
Output
Ready to run.
A range is not a list
Printing a range shows a compact description such as
range(0, 10) rather than all of its values. Converting it with
list(...) creates a list, but the range itself represents the progression using its start, stop, and step information. This is why a range stays small even when it describes many values.
Output
Ready to run.
Range equality follows the represented sequence
Two range objects compare equal when they represent the same sequence of values, even when their start, stop, and step descriptions are different. The comparison is about the sequence represented, not about matching argument triples.
Output
Ready to run.
Choose range when the pattern is the data
Use range when your values form an integer arithmetic progression. Convert it to a list only when you actually need a materialized list. For arbitrary existing values, iterate over the sequence you already have instead of manufacturing indices just because a loop exists.
Use the one-argument form when the progression starts at 0.
Use the two-argument form when you need a different starting point or an explicit stopping point.
Use a negative step for descending progressions.
Keep the range as a range when you need iteration, indexing, membership, or slicing without a materialized list.
You now have a clear model for
range itself. The Loops lesson remains the place for loop control; this lesson owns the built-in sequence type and its behavior. When you are ready, take the Python range quiz.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.