TOPIC 6: Python range ( )

6. Accessing Values in a Range & Slicing a Range

Accessing Values in a Range

Just like lists, you can access values in a range using their index.

Example:

r = range(10)

print(r[2])

Output:

2

Since indexing starts at 0, index 2 contains the value 2.

Slicing a Range

You can also slice a range to get part of the sequence.

Example:

r = range(10)

print(r[:3])

Output:

range(0, 3)

This creates a new range containing the first three values.