0

Why is it okay to be false one way but true the other way using "or"

print(2 == (2 or 1)) True print(False == (False or True)) False "I would thought print(False == (False or True)) would be True" "Thanks for your help" "DJ"

16th Nov 2018, 3:32 PM
Dj Pratt
Dj Pratt - avatar
6 ответов
+ 7
a or b => a if a else b a and b => b if a else a so 2 == (2 or 1) #a is True so returns a 2 == 2 True False == (False or True) #a is False so returns b False == True False
16th Nov 2018, 4:58 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 6
Just so you know (2 or 1) is 2 while (1 or 2) is 1 so print(2 == (1 or 2)) is false.
16th Nov 2018, 4:19 PM
John Wells
John Wells - avatar
+ 3
Dj Pratt if I asked you a math question, what is 2+2 and you weren't sure of the answer but you knew it was 3 or 4, you would be right because the answer is 3 or 4 in this case 4. I hope this helps you to understand, I was confused at that as well when I learned it.
16th Nov 2018, 3:46 PM
Austin Kline
Austin Kline - avatar
+ 3
John Wells whoaa!! I didn't know that. Really cool.
16th Nov 2018, 4:23 PM
Anya
Anya - avatar
+ 2
Dj Pratt . When you have False == (False or True) you are not cheking with False equals False or equals True you are checking with result of operation X or Y is true. So in math True or False equals True. So you kind writting False == True. That's why is False
16th Nov 2018, 4:04 PM
Anya
Anya - avatar
+ 1
I was shocked the original gave true so I coded prints of both versions to find the result is the first number. In other languages, (2 or 1) would give true so 2 == true would be false.
16th Nov 2018, 4:30 PM
John Wells
John Wells - avatar