Range Methods

range.count()

Count elements equal to a supplied value. Since range contains distinct integers, an ordinary numeric value occurs at most once. Exact integers and bool use arithmetic membership in CPython; other objects may require equality comparisons over the sequence, so avoid assuming constant time for every argument. Integral floats can compare equal to an integer element. A custom equality method may even match more than one element. This method does not materialize a list.

Formal Syntax & Structure

python syntaxpython
values.count(value, /)

Parameters & Attributes

NameTypeDescription
valueobjectValue compared with range elements.

Return Value

int

Number of matching elements.

Working Example

Example Snippetpython
slots = range(2, 12, 3)print(slots.count(5))print(slots.count(6))print(slots.count(5.0))
Python Reference