The search method is defined on the list, and the only way to search in an unordered list is by iterating (of course the order in which to iterate is arbitrary, but there is no reason to use another one for another type of data, especially since you can put objects with different types in the same list).
So the list is basically unaware what elements are in it, and iterates over the list like:
# basic implementation of linear search
def __contains__(self, item):
for elem in self:
if item == elem:
return True
return False
Now the only thing that differs is the ==
method (and thus the underlying __eq__
). Indeed: the equality of int's can be calculated more efficiently, since usually 32 or 64 bits can be compared at once (because that is how it is usually hardwired on a processor). This in contrast with strings, where one iterates over the the characters. So equality checks over int
s are faster than the ones over str
ings.
Finally you can encode binary numbers more compact on an integer. For instance 1110
(binary) is equivalent to 14
. You do not have to do the math yourself: as this answer says, you can simply write 0b1110
and Python will convert it to the int 14
.