Why a != b != c is not equal to a != b and a != c and b != c?
NickName:Jironymo Ask DateTime:2019-10-15T21:30:25

Why a != b != c is not equal to a != b and a != c and b != c?

I want to check if 3 values a, b , c are not equal to each other. Given that a == b == c equals to a == b and b == c and a == c, why does python give a different answer for a != b != c ?

Thanks!

This is a task from an introductory course to Python:

"How many of the three integers a, b, c are equal?"

This is a simple task and I got the correct answer with the following:

a = int(input()); b = int(input()); c = int(input());

if a != b and a != c and b != c:
    print(0)
elif a == b == c:
    print(3)
else:
    print(2)

Yet, I can not understand why a != b != c wouldn't do the job in the initial if statement.

From a != b != c I expect the same as from a != b and a != c and b != c

Copyright Notice:Content Author:「Jironymo」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58395948/why-a-b-c-is-not-equal-to-a-b-and-a-c-and-b-c

Answers
Dani Mesejo 2019-10-15T13:38:52

When you use a != b != c you are actually using chained comparison, from the documentation:\n\n\n Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are\n comparison operators, then a op1 b op2 c ... y opN z is equivalent to\n a op1 b and b op2 c and ... y opN z, except that each expression is\n evaluated at most once.\n\n\nSo a != b != c is actually a != b and b != c, which is different from a != b and a != c and b != c, for example:\n\na, b, c = 1, 2, 1\nprint(a != b != c)\nprint(a != b and a != c and b != c)\n\n\nOutput\n\nTrue\nFalse\n",


khalid_salad 2019-10-15T13:33:32

The \"equals\" operator is transitive:\n\n\n if a == b and b == c, then a == c\n\n\nThe \"not equals\" operator is not:\n\n\n if a != b and b != c, a could still equal c\n\n\nWhy? Take \n\n\n a = 3, b = 4, c = 3\n\n\nThen\n\n\n a != b, b != c, but a == c\n",


More about “Why a != b != c is not equal to a != b and a != c and b != c?” related questions

Why a != b != c is not equal to a != b and a != c and b != c?

I want to check if 3 values a, b , c are not equal to each other. Given that a == b == c equals to a == b and b == c and a == c, why does python give a different answer for a != b != c ? Thanks! ...

Show Detail

In C# integer arithmetic, does a/b/c always equal a/(b*c)?

Let a, b and c be non-large positive integers. Does a/b/c always equal a/(b * c) with C# integer arithmetic? For me, in C# it looks like: int a = 5126, b = 76, c = 14; int x1 = a / b / c; int x2 ...

Show Detail

Why is a / (b*c) slower than a / b / c in Python (and in general)?

I stumbled across an interesting optimization question while writing some mathematical derivative functions for a neural network library. Turns out that the expression a / (b*c) takes longer to com...

Show Detail

greatest in number a,b,c

def greater(a,b): if a > b: return a elif a==b: return "equal" else: return b def greatest(a,b,c): if a>b and a>c: return 'a&

Show Detail

How to prove a*b*c=a*(b*c) in Coq?

Im trying to prove the above question. I have been given a definition of an induction: Definition nat_ind (p : nat -> Prop) (basis : p 0) (step : forall n, p n -> p (S n)) : fora...

Show Detail

If b = [1, 2, 3, 4], and c = [...b], why doesn't b equal c?

The title pretty much says it all, but here it is written out: b = [1, 2, 3, 4]; c = [...b]; b === c; //false Why?

Show Detail

question about ? and : in c++

Why this statement : int a = 7, b = 8, c = 0; c = b>a?a>b?a++:b++:a++?b++:a--; cout << c; is not equal to : int a = 7, b = 8, c = 0; c = (b>a

Show Detail

Why does this php code not work if ((a==a and b==b) or (c==c)) {}

I can't find why and/or in one line of if doesn't work it gets a=a and b=b that works but it wont get c=c that doesn't work Example: if ((a==a and b==b) or (c==c)) {} Actual code: if (($_POST[...

Show Detail

SQL Query to Group/SUM Columns C, D, when A, B are equal

I have a MySQL Table that has 4 columns, we will call them C1, C2, C3, and C4. In this data, I have a data set like so: C1,C2,C3,C4 A,B,12,24 C,D,23,13 A,D,19,15 B,C,15,15 A,C,21,9 B,D,19,2 B,A,8...

Show Detail

in JS, are (a===b===c) and ( (a===b)&&(a===c)&&(b===c) ) the same? //sorry for my ignorance

My aim is "to execute the code when only a, b, and c are equal". I figured out two codes: Code#1: if (a===b===c) {console.log('something')}; code#2: if ( (a===b)&&(a===c)&

Show Detail