+ 2

print(0 or 2)

Why print(0 or 2) Outputs: 2 I though that 0 - False, 2 - True so False or True Why is this?

5th Jan 2020, 10:13 AM
my stummy hurts
my stummy hurts - avatar
4 Answers
+ 1
a or b: a if bool(a) else b a and b: a if not bool(a) else b
5th Jan 2020, 6:31 PM
Seb TheS
Seb TheS - avatar
+ 6
0 is a False statement 2 is a True statement The 'or' operator will chose the first correct statement, which is 2
5th Jan 2020, 10:34 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Yannick Daniel Gibson any number different than 0 is considered to be a True statement. if 2: print("first test") if -6: print("second test") if 0: print("third test") OUTPUT: first test second test
5th Jan 2020, 10:46 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Had to think 'bout how operators work, actually, my understanding was a bit off, but now I get it. Thanks!
5th Jan 2020, 10:43 AM
my stummy hurts
my stummy hurts - avatar