Range Methods

range.index()

Return the zero-based position of the first element equal to the requested value, not the element itself. For ordinary integer values, arithmetic can locate the match without scanning all values on CPython. Non-integer objects may use equality-based iteration. A missing value raises ValueError. Unlike list.index(), range.index() does not accept optional search bounds.

Formal Syntax & Structure

python syntaxpython
values.index(value, /)

Parameters & Attributes

NameTypeDescription
valueobjectValue to locate by equality.

Return Value

int

First matching position.

Working Example

Example Snippetpython
slots = range(20, 0, -4)print(slots.index(12))print(slots.index(4))try:    slots.index(7)except ValueError:    print("missing")
Python Reference