mardi 4 septembre 2018

Why does Pythons logical or behave like this? [duplicate]

This question already has an answer here:

Sorry if this has been asked before. I've tried googling this and found a few very complicated answers that I don't understand properly, so can anyone maybe eli5 this for me?

n = 5

if(n == 1 or 2 or 3):
    print "n < 4"
elif(n == 4 or 5):
    print "n == 4 or 5"
else:
    print "n > 5"


x = 5

if(x == (1 or 2 or 3)):
    print "x < 4"
elif(x == (4 or 5)):
    print "x == 4 or 5"
else:
    print "x > 5"


y = 5

if((y == 1) or (y == 2) or (y == 3)):
    print "y < 4"
elif((y == 4) or (y == 5)):
    print "y == 4 or 5"
else:
    print "y > 5"

I don't get how this works. Why are they all outputting something different?

Aucun commentaire:

Enregistrer un commentaire