Why does the range have a "count" function?

If I am wrong, the range can only include unique numbers. So the number may or may not be in it.

My guess is that if we want to pass a range as one of the many accepted types (dynamic) for some function, that makes sense.

However, it looks like if it were to move the entire range to count the number it would be very inefficient.

I tried to compare it to " in

" but on my machine it seems like they have the same timing. Could this be correct?

+3


source to share


1 answer


Because objects range()

correspond to Sequence

ABC
and that ABC has a method .count()

.

In other words, it exists for completeness, so that the object qualifies as a sequence .



It is not necessary to iterate over the whole range, as it is easy enough to calculate if this number is part of a sequence, see Why is "1000000000000000 in the range (1000000000000001)" so fast?

+5


source







All Articles