0

I was trying to check if a list is equal to either of the two target lists ; It did not work using or operator . What am I missing here and how to use use or operator in the below scenario?

 k = [0 for i in range(9)]
    k[2] = 1

    if [k[i] for i in [0,1,2] ] == [0,0,0] or [0,1,1]  : print ("yup")

    else : print("NO")

result was yup while it should have been NO

#but worked when used elif

 k = [0 for i in range(9)]
    k[2] = 1

    if [k[i] for i in [0,1,2] ] == [0,0,0]   : print ("yup")
    elif [k[i] for i in [0,1,2] ] == [0,1,1]   : print ("yup")
    else : print("NO")
  • `a == b or c` is equivalent to `(a == b) or c`, not `(a == b) or (a == c)`. – luk2302 Dec 09 '21 at 12:52
  • I don't get it, why is this question asked so often? In what way would it possibly make sense for `a == b or c` to mean the same thing as `(a == b) or (a == c)`? I don't know of a single programming language, or even mathematical formalism, where that's the case. – joanis Dec 09 '21 at 13:43
  • @luk2302 got it , thanks ! ; – hobby_coder Dec 09 '21 at 14:03
  • @joanis ; Ya ! now it seems silly of me – hobby_coder Dec 09 '21 at 14:08
  • Well, @hobby_coder, it's a common question, so while I don't get it, you shouldn't feel too bad about it. Many others seem to have had the same intuition as you. – joanis Dec 09 '21 at 14:10

0 Answers0