range() represents an arithmetic sequence of integers for iteration. Its stop value is excluded, and a single argument supplies a stop with an assumed start of 0. section 11.1.1
Use range() to:
- Repeat an action a chosen number of times, using _ when the generated integer is irrelevant. section 9.3.5
- Generate indexes for a sequence, such as character positions passed to random.sample(). section 10.1.3
- Count upward or use a negative step to count downward; reversed(range(...)) provides another reverse-iteration option. section 11.1.1
Exclusive stop and reverse steps
list(range(5)) produces [0, 1, 2, 3, 4]. list(range(1, 5)) produces [1, 2, 3, 4], while list(range(5, 0, -1)) produces [5, 4, 3, 2, 1]. Swapping start and stop without a negative step produces an empty sequence. section 11.1.1
Iteration without using the values
num_insults = 2 for _ in range(num_insults): print('An insult!') This prints the line twice. The underscore documents that each generated value is intentionally discarded. section 9.3.5
Inspect the generated values and see the exclusive stop directly in a REPL-style example. section 11.1.1








