Boolean-logic in Python | Sololearn: Learn to code for FREE!


Boolean-logic in Python

I wondered - in python, are numbers not equal to 0 equal True? I decided to check this out and this is what happened: print(1 if 2 else 0) >>> 1 print(1 if 10 else 0) >>> 1 print(1 if -1 else 0) >>> 1 But! print(1 == True) >>> True print(-1 == True) >>> False Can someone answer my question?

7/5/2020 3:12:10 PM


6 Answers

New Answer


if <any number> or <any variable> is always true, if the value is set. It's kind a not null-condition. The integer representation of a boolean is different from that and this is, what you checked with the last both statements. Only 1 is equal to true.


Sandra Meyer, Martin Taylor, Thank you) sorry that I can not mark both answers as the best


read my name if you know the answer


Can you explain not operator clearly


im confused-