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

  • Context: Python 
  • Thread starter Thread starter Bipolarity
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around the behavior of Python 3's print statement when evaluating the expression print(1!=0==0). Participants are exploring the order of operations and the implications of comparison chaining in Python, with a focus on understanding how the expression evaluates to 'True'.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions whether the expression evaluates (1!=0) first, (0==0) first, or if it separates them into a different form.
  • Another participant suggests that Python uses a left to right evaluation rule due to the same precedence of the operators involved.
  • Some participants explain that Python's comparison chaining allows expressions like 0
  • There is a clarification that the expression is not equivalent to a logical XOR operation, but rather involves a more complex evaluation that can lead to different function call behaviors depending on how the expression is structured.
  • A participant highlights the potential side effects of rewriting the expression in a different form, which could lead to multiple evaluations of a function.

Areas of Agreement / Disagreement

Participants express differing views on the exact nature of the evaluation process, with some agreeing on the concept of comparison chaining while others debate the implications and equivalences of the expressions involved. No consensus is reached on the best way to interpret the evaluation.

Contextual Notes

Participants note that the evaluation of chained comparisons can lead to different behaviors, particularly regarding function calls and side effects, which may not be immediately apparent from the syntax alone.

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.
 

Similar threads

  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
5
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K