I'm a little confused about the python in operator for sets.
If I have a set s and some instance b, is it true that b in s means "is there some element x in s such that b == x is true"?
I'm a little confused about the python in operator for sets.
If I have a set s and some instance b, is it true that b in s means "is there some element x in s such that b == x is true"?
Yes, but it also means hash(b) == hash(x), so equality of the items isn't enough to make them the same.
Yes it can mean so, or it can be a simple iterator. For example: Example as iterator:
a=set(['1','2','3'])
for x in a:
print ('This set contains the value ' + x)
Similarly as a check:
a=set('ILovePython')
if 'I' in a:
print ('There is an "I" in here')
edited: edited to include sets rather than lists and strings
Strings, though they are not set types, have a valuable in property during validation in scripts:
yn = input("Are you sure you want to do this? ")
if yn in "yes":
#accepts 'y' OR 'e' OR 's' OR 'ye' OR 'es' OR 'yes'
return True
return False
I hope this helps you better understand the use of in with this example.