-2

Can anyone explain, What exactly the exp1 and exp2 are doing? Kindly separate each case so it can understandable.

a, b, c, d, e, f = 0, 5, 12, 0, 15, 15

exp1 = a <= b < c > d is not e is f 
exp2 = a is d > f is not c 

print(exp1) 
print(exp2) 
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • You can read about comparison chaining [in the docs](https://docs.python.org/3/reference/expressions.html#comparisons). – jonrsharpe Feb 17 '20 at 08:19
  • Read the first one as `exp1 = (a <= b) and (b < c) and (c > d) and (d is not e) and (e is f)`. – Matthias Feb 17 '20 at 08:23
  • And the usage of `is` might lead to strange looking results here. If you do `a, b, c, d, e = 0, 5, 12, 0, 1500` and on the next line `f = 1500` you'll get a different result for `exp1` (at least in IDLE). – Matthias Feb 17 '20 at 08:29
  • @Matthias Yeah absolutely, `is` and `==` are not the same. Beginners tend to mix them up. – Ch3steR Feb 17 '20 at 08:31

1 Answers1

3

exp1 = a <= b < c > d is not e is f is evaluated as a<=b and b<c and c>d and d is not e and e is f.

Byte-code for both expressions is the same (dis module reference).

In [8]: import dis

In [9]: dis.dis('a <= b < c > d is not e is f')
  1           0 LOAD_NAME                0 (a)
              2 LOAD_NAME                1 (b)
              4 DUP_TOP
              6 ROT_THREE
              8 COMPARE_OP               1 (<=)
             10 JUMP_IF_FALSE_OR_POP    48
             12 LOAD_NAME                2 (c)
             14 DUP_TOP
             16 ROT_THREE
             18 COMPARE_OP               0 (<)
             20 JUMP_IF_FALSE_OR_POP    48
             22 LOAD_NAME                3 (d)
             24 DUP_TOP
             26 ROT_THREE
             28 COMPARE_OP               4 (>)
             30 JUMP_IF_FALSE_OR_POP    48
             32 LOAD_NAME                4 (e)
             34 DUP_TOP
             36 ROT_THREE
             38 COMPARE_OP               9 (is not)
             40 JUMP_IF_FALSE_OR_POP    48
             42 LOAD_NAME                5 (f)
             44 COMPARE_OP               8 (is)
             46 RETURN_VALUE
        >>   48 ROT_TWO
             50 POP_TOP
             52 RETURN_VALUE

In [10]: dis.dis('a<=b and b<c and c>d and d is not e and e is f')
  1           0 LOAD_NAME                0 (a)
              2 LOAD_NAME                1 (b)
              4 COMPARE_OP               1 (<=)
              6 JUMP_IF_FALSE_OR_POP    38
              8 LOAD_NAME                1 (b)
             10 LOAD_NAME                2 (c)
             12 COMPARE_OP               0 (<)
             14 JUMP_IF_FALSE_OR_POP    38
             16 LOAD_NAME                2 (c)
             18 LOAD_NAME                3 (d)
             20 COMPARE_OP               4 (>)
             22 JUMP_IF_FALSE_OR_POP    38
             24 LOAD_NAME                3 (d)
             26 LOAD_NAME                4 (e)
             28 COMPARE_OP               9 (is not)
             30 JUMP_IF_FALSE_OR_POP    38
             32 LOAD_NAME                4 (e)
             34 LOAD_NAME                5 (f)
             36 COMPARE_OP               8 (is)
        >>   38 RETURN_VALUE

exp2 = a is d > f is not c is evaluated as a is d and d>f and f is not c. They both would produce the same byte-code.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58