Posts: 22
Threads: 9
Joined: Oct 2016
Oct-11-2016, 11:58 PM
(This post was last modified: Oct-12-2016, 12:08 AM by Yoriz.)
Hello,
Newbie question here, which is the correct way to use the and operator if checking to see whether two values equal a third value:
if a == b and c == b:
do something
if a and c == b:
do something
if b in (a,c):
do something
Posts: 2,164
Threads: 35
Joined: Sep 2016
Oct-12-2016, 12:06 AM
(This post was last modified: Oct-12-2016, 12:07 AM by Yoriz.)
The first one would work.
The second would always be true regardless of value a if b and c where equal.
The third would be true if b was either a or c
Posts: 5,150
Threads: 396
Joined: Sep 2016
This suspiciously sounds like a test question.
Recommended Tutorials:
Posts: 22
Threads: 9
Joined: Oct 2016
Thanks. It's not a test question. I was not sure how to see if two values equal the same value. The second option makes the most sense to me, but I forget you have to compare each value to the third value as in the first option.
Posts: 591
Threads: 26
Joined: Sep 2016
I believe you can chain the == operator without an and with no issues:
if a == b == c:
Posts: 8,090
Threads: 154
Joined: Sep 2016
Oct-12-2016, 07:29 AM
(This post was last modified: Oct-12-2016, 07:30 AM by buran.)
(Oct-12-2016, 12:06 AM)Yoriz Wrote: The second would always be true regardless of value a if b and c where equal.
Not if a is None, 0, empty list, empty tuple, empty dict, etc. i.e. everything that would evaluate to FALSE
a=None
b=1
c=1
print 'a={}, b={}, c={}'.format(a,b,c)
if a and b == c:
print 'It is TRUE'
else:
print 'It is FALSE' Output: a=None, b=1, c=1
It is FALSE
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Oct-12-2016, 04:07 AM)Mekire Wrote: I believe you can chain the == operator without an and with no issues:
if a == b == c:
Really? Hmm.... >>> a = 0
>>> b = 0
>>> c = 0
>>> a == b == c
True I'm not sure that should be used in practice, as when I saw that I immediately thought it'd fail, since in my mind (a==b) becomes a bool, and c!=True.
Posts: 4,229
Threads: 97
Joined: Sep 2016
(Oct-17-2016, 06:48 PM)nilamo Wrote: I'm not sure that should be used in practice, as when I saw that I immediately thought it'd fail, since in my mind (a==b) becomes a bool, and c!=True.
Ah, but then this wouldn't work either:
if -10 < x < -2: Also note:
>>> a, b, c = 0, 0, 0
>>> (a == b) == c
False
Posts: 1,298
Threads: 38
Joined: Sep 2016
(Oct-17-2016, 08:39 PM)ichabod801 Wrote: (Oct-17-2016, 06:48 PM)nilamo Wrote: I'm not sure that should be used in practice, as when I saw that I immediately thought it'd fail, since in my mind (a==b) becomes a bool, and c!=True.
Ah, but then this wouldn't work either:
if -10 < x < -2: Also note:
>>> a, b, c = 0, 0, 0
>>> (a == b) == c
False
Doesn't the order of precedence still apply and the parenthesis just become redundant? I mean obviously they don't, but why? Never mind... took me a moment to wrap my head around what was happening
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Oct-17-2016, 09:00 PM)sparkz_alot Wrote: Never mind... took me a moment to wrap my head around what was happening
Exactly why I don't think that should be used :p
|