This is a simple snippet of code that I have:
>>> test = ['asas', 'asa12', 'sdsdsd12', 'vccvcvcv', 'rertrtt11']
>>> for i in test:
... if '11' in i:
... print i
...
rertrtt11
Works as expected.Now to catch elements containing either 11 or 12,I do this :
>>> test = ['asas', 'asa12', 'sdsdsd12', 'vccvcvcv', 'rertrtt11']
>>> for i in test:
... if '11' or '12' in i:
... print i
...
asas
asa12
sdsdsd12
vccvcvcv
rertrtt11
That was unexpected.I was hoping to catch only the elements with 11 or 12 in it but the output contained all the elements.
While trying to troubleshoot,I tried an and operator to try to understand the behaviour:
>>> test = ['asas', 'asa12', 'sdsdsd12', 'vccvcvcv', 'rertrtt11']
>>> for i in test:
... if '11' and '12' in i:
... print i
...
asa12
sdsdsd12
Now I am thoroughly confused as I was expecting an empty set since there are no elements with both 11 and 12 in them.Is my understanding of operator functioning in python completely wrong ??