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

+1

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

Granger

6 Answers

New Answer

+1

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.

0

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

0

read my name if you know the answer

0

Can you explain not operator clearly

-1

It's the way Python evaluates the expression. Anything that evaluates to 0, or null is False, while everything else is True. However, when you do a literal comparison Then False=0 and True=1. print(1==True) >>> True print(2==True) >>> False print(0==False) >>> True print(-1==False) >>> False

-1

im confused-