Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `a<b<c` valid python?

Tags:

python

I was curious to see if I could use this a<b<c as a conditional without using the standard a<b and b<c. So I tried it out and my test results passed.

a = 1
b = 2
c = 3

assert(a<b<c) # In bounds test
assert(not(b<a<c)) # Out of bounds test
assert(not(a<c<b)) # Out of bounds test

Just for good measure I tried more numbers, this time in the negative region. Where a, b, c = -10, -9, -8. The test passed once again. Even the test suit at a higher range works a, b, c = 10, 11, 12. Or even a, b, c = 10, 20, 5.

And the same experiment done in C++. This was my mentality going into it:

#include <iostream>

using namespace std;

int main()
{
    int a,b,c;
    a=10;
    b=20;
    c=5;
    cout << ((a<b<c)?"True":"False") << endl; // Provides True (wrong)
    cout << ((a<b && b<c)?"True":"False") << endl; // Provides False (proper answer)
    return 0;
}

I originally though that this implementation would be invalid since in every other language I have come across would evaluate a boolean before it would reach c. With those languages, a<b would evaluate to a boolean and continuing the evaluation, b<c, would be invalid since it would attempt to evaluate a boolean against a number (most likely throwing a compile time error or falsifying the intended comparison). This is a little unsettling to me for some reason. I guess I just need to be reassured that this is part of the syntax. It would also be helpful to provide a reference to where this feature is provided in the Python documentation so I can see to what extent they provide features like this.

like image 654
jakebird451 Avatar asked Sep 12 '13 03:09

jakebird451


People also ask

Is ABC valid in Python?

But, don't try to keep a super long identifier, it will only hurt your credibility as a programmer. Python identifier names are case-sensitive. So, “abc” and “ABC” are two different identifiers. It's best to use small cases for identifiers for uniformity across your programs.

Is a B and B a the same in Python?

It appears that a, b = b, a involves the one-to-one unpacking. However, it turns out that Python uses an optimized operation (i.e., ROT_TWO ) to swap the references on a stack. Such swapping happens when three variables are involved. However, tuple creation and unpacking come to play when there are four variables.

What does a b/b a/b mean in Python?

Let's grok it. a, b = b, a + b. It's a tuple assignment, means (a, b) = (b, a + b) , just like (a, b) = (b, a) Start from a quick example: a, b = 0, 1 #equivalent to (a, b) = (0, 1) #implement as a = 0 b = 1.

Can you do a == b == c in Python?

No, there aren't any side-effects.


2 Answers

This is documented here.

Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

And, as an example,

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

like image 76
Sukrit Kalra Avatar answered Nov 15 '22 10:11

Sukrit Kalra


Python chains relational operators "naturally". Note that Python's relational operators include in and is (and their negatives), which can lead to some surprising results when mixing them with the symbolic relational operators.

like image 20
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 09:11

Ignacio Vazquez-Abrams