index and len of a range

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functional programming in Python

index and len of a range

Similar to strings, lists, and tuple, but unlike filehandles, we can use the len function on the range object and we can also access a specific element using the square-bracket indexing without actually iterating over all the elements.

rng = range(10, 10000, 2)

print(rng[2])
print(len(rng))

Output:

14
4995