Python Why does Python 3's print(1!=0==0) return 'True'?

  • Thread starter Thread starter Bipolarity
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The expression print(1!=0==0) evaluates to 'True' due to Python's comparison chaining feature, which allows multiple comparisons to be evaluated in a single expression. The evaluation begins from the left, first determining if 1 is not equal to 0, which is True, and then checking if that result is equal to 0, which is False. Thus, the overall expression evaluates to True. The discussion emphasizes the importance of using parentheses to clarify the order of operations and avoid ambiguity. It also highlights that while chaining comparisons can be useful, it can lead to different behaviors in function calls, particularly regarding how many times a function is executed, which is crucial when side effects are involved.
Bipolarity
Messages
773
Reaction score
2
print(1!=0==0) seems to print the value 'True'. I'm trying to understand why.

Does it first evaluate (1!=0) ?

Or does it first evaluate (0==0) ?

Or does it separate them into (1!=0)^(0==0) ?

I appreciate all help.

BiP
 
Technology news on Phys.org
Not sure but I think it would use the left to right rule since != and == would have the same precedence as operators.

It's best to write code without an ambiguity like this by explicitly using parenthesis to order the sequence of operations.
 
Bipolarity said:
Or does it separate them into (1!=0)^(0==0)
That's what it does. There's some python comparison chaining magic going on here to enable expressions such as 0<x<1 to mean x is between 0 and 1 -- just like one would write in math. That you can use it for 1!=0==0 is a side effect of this magic.
 
Or does it separate them into (1!=0)^(0==0) ?

D H said:
That's what it does. There's some python comparison chaining magic going on here to enable expressions such as 0<x<1 to mean x is between 0 and 1 -- just like one would write in math. That you can use it for 1!=0==0 is a side effect of this magic.

Thanks for the tip on the 0<x<1 syntax DH. That's useful to know. :)

Though surely it's equivalent to (1!=0) and (0==0), rather than xor.
 
uart said:
Though surely it's equivalent to (1!=0) and (0==0), rather than xor.
Correct. I read the ^ as the mathematical shorthand for boolean and, which is not the case.It's not quite equivalent to (1!=0) and (0==0). There is a subtle difference. Consider
self.inbounds = 0 < self.some_function() < 1

This will call self.some_function() once and only once. Now let's rewrite this expression as
self.inbounds = (0 < self.some_function()) and (self.some_function() < 1)

With this rewrite, self.some_function() will be called once if the result from the first call is non-positive, twice if it is positive. Calling the function sometimes once, sometimes twice, can be deleterious if the function has side effects.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top