hello,
I am a beginner in python. I just started learning python in school 3 days ago.
I was wondering why this becomes true and false:
0 == (1 == 2)
true
0 == 1 == 2
false
what difference do the brackets make so it becomes true?
0 == ( 1 == 2 )
true
( 1 == 2 ) evaluates to False. 0 == False
evaluates to True because 0, None and an empty value in Python are False.
0 == 1 == 2
False
0 == 1 evaluates to False. False == 2
evaluates to False because all numbers which are not 0 are evaluated to True. So False is not euqal to True
(Sep-06-2017, 04:17 PM)wavic Wrote: [ -> ]0 == False evaluates to True because 0, None and an empty value in Python are False.
This isn't quite correct.
0
and
False
are the same value (not the same object).
False
is technically a Boolean type but it is indistinguishable from
0
for all practical purposes.
None
and empty sequences on the other hand are falsey values, but they are not
equal to
False
(or
0
).
>>> 0 == False
True
>>> None == False
False
>>> () == False
False
>>>
This is what I mean:
>>> bool(0)
False
>>> if 0:
... print('True')
...
>>> bool(None)
False
>>> if None:
... print('True')
...
>>> bool([])
False
>>> if []:
... print('True')
...
>>>
Quite aware of that, however the way you stated it (perhaps due to language) was not accurate. His used neither bool
nor a syntax which forced the expression to be evaluated in a boolean context.
Just as 2 == True
is a false statement regardless of the fact that 2 would evaluate to True in a boolean context.
Yes, perhaps because my the English is not my native language. However, what I've said is that it evaluates to True or False. Not that is True or False.