0<0==0 is false in python Why ??? | SoloLearn: Learn to code for FREE!

+6

# 0<0==0 is false in python Why ???

Python boolean problem

12/19/2019 11:38:46 PM

Ayush Kumar

+31

This is called 'chaining'. The expression is translated to: 0<0 and 0==0

+14

Python does it differently than for example C. In C you always have to write stuff like... lowerEdge<middle&&middle<upperEdge ... if you wanted to check that the middle is actually in the middle. Python has the shortcut: lower < middle < upper And this is read like lower<middle AND middle<upper in C. 0<0==0 would go like this: 0<0 and 0==0 False and True False

+13

Mirielle🐶 [Inactive], the operators that can be chained in this way are all of the same precedence and will be evaluated from left to right. It's these ones: in, not in, is, is not, <, <=, >, >=, !=, ==

+9

Mirielle🐶 [Inactive] Ok if you think i am wrong fine. From my side I know exactly what i am saying. And i hope one day you will understand the difference between precedence (grouping of operands in a multi operator expression) and evaluation order of subexpressions in an expression.

+9

That's right, Mirielle🐶 [Inactive], god forbid anyone ever told *you* what to do! Don't even try to once listen and reflect your own behavior. I seriously pity swim, who's always investing a lot of energy to help people here. If someone told me 'you're wrong, I know everything better, you don't understand this, but I have only read 1% of what you wrote', I'd just fall silent.

+8

Mirielle🐶 [Inactive] How you evaluate in Maths does not always maps to how different language compiler evaluates expressions but it is guaranteed PEDMAS or BODMAS rules are respected. Coming to your example (2+2) + (2+2) = 8 let (2+2) left of middle '+' be 'A' and other (2+2) be 'B' So you have A + B = 8 Do you know for sure out of A and B which one will be evaluated first in Java,Python or in C/C++ ? Let A be further equivalent to A = a1 + a2 and B = b1 + b2. In A out of a1 or a2 which one will be evaluated first in Java, Python or C/C++? Same for b1 and b2 ? In strict left to right order of evaluation A will be evaluated before B and a1 will be evaluated before a2 and b1 before b2 this will happen in Java and Python but in C/C++ B can evaluate before A and a2 before a1 and b2 before b1 and what's more for the next run of your program, the order can change completely or part of it. That's C/C++ ! for 2+2+2+2+(5+5) bracks only says evaluate/add (5+5) and the result of it to the rest without it the expression will be evaluated as ((((2+2)+2)+2)+5)+5) with brackets it will be evaluated as (((2+2)+2)+2)+sumof(5+5)) In a language like Java and Python but it will be evaluated left to right. But in C/C++, the compiler can see the expression as (2+2+2+2)+(5+5) = A + B where B = (5+5) and A as rest of the expression. Again which of the A or B will be evaluated first, you can't be sure. However even here A will be evaluated as (((2+2)+2)+2) because of precedence and associativity rules.

+8

Mirielle🐶 [Inactive], seriously, if you have no time engaging in a proper discussion, why are you discussing in the first place? You're just wasting everybody's time, including your own, if you just pick any line from what the other person said, and hip-shoot at it. Just wait until you have the time. Then read. Then think. And only then answer.

+7

Petr, I've found some article: https://www.geeksforgeeks.org/chaining-comparison-operators-python/

+7

Precedence does NOT governs the order of evaluation. Precedence is always about grouping of operands in an expression with multiple operators. When the precedence is same then the associativity (left to right or right to left grouping) rules are looked up.. Order of evaluation is language specific. Most follows left to right order of evaluation. Where as C/C++ does not define order of evaluation for most operations.

+7

Mirielle🐶 [Inactive] I did not expected this from you, you commented based on one line which you think is wrong (which however is not) not even reading the comment. I didn't replied to you after reading your previous comment, i felt like i was wasting my time. I don't know how should i react to your comments in future, because now i have no idea whether you have read the comments or you are commenting based on some line you think is wrong. Based on how you have commented to my comment i think HonFu is right in asking you to read the comments properly, understand it and then answer.

+7

Ayush Kumar If I may, I'd like to expand on the selected answer posted by HonFu and address some of the other questions that followed. First, chained comparisons are one of the few features I actually like about Python. Consider the following: 0 < 0 == 0 Produces disassembled bytecode equivalent to: 0 < 0 and 0 == 0 The second comparison will not be evaluated because it will be short circuited by the first comparison, which evaluates to false. Now, consider: (0 < 0) == 0 or 0 < (0 == 0) Neither of these are chained comparisons because the parentheses make these equivalent to: (group) == 0 or 0 < (group) As reflected in the disassembled bytecode. It should also be noted that chained comparisons apply to comparison operators rather than arithmetic operators. Therefore, arithmetic order of operations and groups would not apply to chained comparisons. You can review and compare the disassembled bytecode: https://code.sololearn.com/coEUvJh40ufQ/ I hope this was helpful.

+6

Mirielle🐶 [Inactive], this is becoming more and more silly really. No one forces you to read anything - just as no one forces you to even answer. Which you shouldn't, if you have no time, energy, and also no interest really. IF you answer, you should read and think first. In a discussion, you're supposed to listen to the argumentation of the other person and react to it, not just randomly pick any line. This makes any debate utterly futile and leads to no result. Try it in your college course, tell the professor: 'Nah, I didn't agree with your first sentence, so I just zoned out and stopped to listen. But what you said is wrong anyway.' I will now stop discussing about this, because you are clearly unwilling or unable to accept valid criticism and reconsider your behavior.

+6

Selin Genkur, did I 'win'? I can't even remember! Because thanks to the lady lalala-finger-in-my-ears I can't read any of it anymore. :-P I believe she should work at the vulnerability. That attitude won't fly anymore as soon as she leaves college.

+5

Forgot to mention one very important fact about precedence and order of evaluation which is Predence is a "Compile time" concept i.e grouping/binding of operands happens at compile time in statically typed languages. In dynamically typed interpreted languages. it happens mostly at the time of parsing the expression but before byte code generation. Order of evaluation is a "Runtime" concept.

+5

If there is any reason for that intense love, I wouldn't ever know since I can't read anything written by she who can't be criticized.

+4

HonFu is there a section in the course (python or other) that describes this situation and the term "chaining"?

+3

Mirielle🐶 [Inactive]'s variations would play out like this: (0<0)==0 (False)==0 0==0 True (because False, as a number is 0) 0<(0==0) 0<(True) 0<1 True, because True as a number counts as 1

+3

that's an everyday maths Anything inside the parenthesis would be evaluted first. I don't use the default operator precedence in any of my thing , so that's what i normally use( so i can have full controls) (0) < (0==0) 0 < True 0 < 1 # True (0 < 0) = 0 False = 0 # True

+3

I always admired her brilliance and confidence. Now I see she doesn't like to admit when she's wrong, and I think that's a vulnerability. I thought she needed some love in this situation :D Btw, I love you too, HonFu :) But you won, so you didn't needed it in this situation :)

+3

~ swim ~ https://imgur.com/a/aVtonWq