Python Forum
Using the and operator to check if two values equal a third
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the and operator to check if two values equal a third
#1
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
Reply
#2
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
Reply
#3
This suspiciously sounds like a test question.
Recommended Tutorials:
Reply
#4
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.
Reply
#5
I believe you can chain the == operator without an and with no issues:
if a == b == c:
Reply
#6
(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
Reply
#7
(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.
Reply
#8
(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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 517 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Check if two matrix are equal and of not add the matrix to the list quest 3 859 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  is there equal syntax to "dir /s /b" kucingkembar 2 1,012 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Can a variable equal 2 things? Extra 4 1,525 Jan-18-2022, 09:21 PM
Last Post: Extra
  Python greater than equal to comparison operator Ilangos 4 2,452 Sep-26-2020, 03:53 AM
Last Post: buran
  Check for specific values on screen and2handles 2 2,405 Jun-17-2020, 05:24 AM
Last Post: and2handles
  Not equal a dictionary key value bazcurtis 2 1,953 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,306 Mar-20-2019, 08:01 PM
Last Post: stillsen
  How many variables/values are limited to single assignment operator? Prabakaran141 1 2,053 Sep-06-2018, 03:32 PM
Last Post: Larz60+
  Misunderstanding with the “if” statement and “not equal” scriptoghost 6 4,456 Jun-23-2017, 09:43 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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