[Tutor] using If statement with chaining of conditional operators

Alan Gauld alan.gauld at yahoo.co.uk
Wed Sep 9 05:23:02 EDT 2020


On 09/09/2020 07:55, Manprit Singh wrote:
>>>> max(a, b, c)
> 7
> Which is the right answer.
> 
> now if i have to solve this using if statement , I would prefer to write
> this solution as given below :

> if b < a > c:
>     print(a)
> elif b > c:
>     print(b)
> else:
>     print(c)

> I have seen this if clause is written in
> several places as given below :
> if (a > b) and (a > c):
>     print(a)

Most programming languages do not permit the B < a > c style
that Python allows. In those languages you must use the twin
test as above. Some folks (like me) coming from other
languages have that style burned into their brain and
so use it even in Python. Either because its what they
are used to seeing or because they forgot that Python
can chain comparisons.

> Need your suggestions, what to prefer in this particular case.

Simplicity is always best to max() wins.
Next best is the compound comparison, and if all else
fails the separate compare.

> The problem is there are 2 variables a and b , each variable is assigned a
> number by the user. Now if the number assigned to both variables is 3 then
> i have to increase the value of both variables by 2  and do nothing if the
> number assigned to both variables is not 3 , and at last print the value of
> both variables
> The code is written below :
> 
> a = int(input("input 1st number"))
> b = int(input("Input 2nd number"))
> if a == 3 == b:
>     a, b = a +2, b + 2
> print(a, b)

Yes, that works.

> if the values assigned to both variables a and b is None, then assign 0 to
> both variables else do nothing , and at last print the values of both
> variables.I would prefer to write the if statement for this problem as
> follows :
> 
> if a is None is b:
>     a, b = 0, 0

And that work also. I assume you tried it right?

but note that 'is' only works for the None case because None is a
singleton object. In the general case you must use ==.
== works for None too...

> Third case where i need your guidance is, if i have 4 variables a, b, c, d
> and each variable is assigned a value , now if the number assigned to all
> these variable is 3 then i have to increase the value of all these values
> by 2, else do nothing. And at last print values of all these values.
> 
> a = int(input("input 1st number"))
> b = int(input("Input 2nd number"))
> c = int(input("Input 3rd number"))
> d = int(input("Input 4th number"))
> if a == b == c == d == 3:
>     a, b, c, d = a + 2, b + 2, c + 2, d + 2
> print(a, b, c, d)

It will work but its getting messy.
I might be considering moving to a list comprehension

if a == b == c == d == 3:
   nums = [n+2 for n in a,b,c,d]
print(*nums)

> See, the point which is giving problem to me is, in the if block of the
> code just written above , first checks that all variables are equal and
> then at last it is checking that all variables are equal to 3 , that logic
> seems little odd to me. kindly correct if my views are wrong.

You can put the order any way you like so if you prefer

if 3 == a == b == c == d:

Or you can expand the test

if a == 3 and
   b == 3 and
   c == 3 and
   d == 3:

It's all about choosing the form that you think is easiest to
comprehend and maintain. Personally I think your first version
is as good as any. So long as you don't put the 3 in the middle
where it would be hidden by the variables.

But if the tests were not all for a single value(3), which
is a far more common scenario,  then the last style would
be clearer to read and easier to amend later.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list