bhylu.com

Advertisements

Range

range describes an integer progression without storing every value in a list. Its stop boundary is excluded.

Understand the idea

One argument supplies a stop with a default start of zero. Two arguments supply start and stop, and a third supplies a nonzero step. A negative step supports a descending progression when the boundaries agree. Convert to a list only when you actually need a materialised list for display or another operation.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

print(list(range(2, 9, 2)))
print(list(range(3, 0, -1)))

Read the result

The results are [2, 4, 6, 8] and [3, 2, 1].

Try one change

Choose a range that includes zero in the descending example, then explain its excluded stop value.