Use and instead of &&.
Videos
Can somebody please explain to me why Python works the way it does when using 'or' statements.
For example I have put together a simple example where I want to return results if the value 1 is in part of an input .
I was trying to be simplistic and say (if test[0] OR test[1]) == 1 then return result.
However, it appears that Python is not treating the two items in the brackets as I had hoped /expected.
It appears that I need to check each item individually e.g.
if test[0]==1 or test[1]==1:
and this does return the correct result.
some simple code to show what I am talking about
j = ["00","01","02","10","11","22"]
print (j)
print('if test[0] == "1" or test[1] == "1":')
for test in j:
if test[0] =="1" or test[1] =="1":
print (test)
print ('if test[0] or test[1] == "1":')
for test in j:
if test[0] or test[1] =="1":
print (test)
print('if (test[0] or test[1]) =="1":')
for test in j:
if (test[0] or test[1]) =="1":
print (test)which gives this output.
You can see that 01,10,11 contain a 1 in some position of the value and these are the items I want returned.
Only one of the 3 methods will return this result, whilst the other 2 methods either return too much or not enough data.
['00', '01', '02', '10', '11', '22']
if test[0] == "1" or test[1] == "1":
01 10 11
if test[0] or test[1] == "1":
00 01 02 10 11 22
if (test[0] or test[1]) =="1":
10 11