Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does this mean?
#1
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?
Reply
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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
>>>
Reply
#4
This is what I mean:

>>> bool(0)
False
>>> if 0:
...     print('True')
...
>>> bool(None)
False
>>> if None:
...     print('True')
...
>>> bool([])
False
>>> if []:
...     print('True')
...
>>>
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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.
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020