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

  • Python
  • Thread starter Bipolarity
  • Start date
  • Tags
    Python
In summary, the conversation discusses the precedence of operators in the expression (1!=0==0) and how Python uses comparison chaining magic to evaluate expressions such as 0<x<1. It is recommended to use parentheses to avoid ambiguity in such cases. However, the use of parentheses in this case could lead to multiple function calls, which can be problematic if the function has side effects.
  • #1
Bipolarity
776
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 

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

What is Python 3?

Python 3 is the latest version of the Python programming language, released in 2008. It is an open-source, high-level, and general-purpose programming language that is widely used for web development, data analysis, artificial intelligence, and scientific computing.

What are the main differences between Python 2 and Python 3?

The main differences between Python 2 and Python 3 are:

  • Python 3 uses a print() function whereas Python 2 uses a print statement
  • Python 3 has stricter syntax rules and is more consistent
  • Python 3 uses Unicode for strings by default
  • Python 3 has improved libraries and better support for asynchronous programming
  • Python 2 is no longer being developed and will not receive updates, while Python 3 is actively maintained

How do I install Python 3?

To install Python 3, you can visit the official Python website and download the installation file for your operating system. There are also many tutorials and guides available online that can help you with the installation process.

Can I still use my Python 2 code in Python 3?

While Python 3 and Python 2 have some differences in syntax and libraries, there are tools available to help with the transition from Python 2 to Python 3. These include tools like 2to3, which automatically converts Python 2 code to Python 3 code, and the future module, which allows you to write code that can run on both Python 2 and Python 3.

What are the advantages of using Python 3?

Some of the advantages of using Python 3 include:

  • Improved syntax and consistency
  • Better support for Unicode and internationalization
  • Enhanced libraries and modules for web development, data analysis, and artificial intelligence
  • Active community and frequent updates and improvements
  • Compatibility with newer technologies and frameworks

Similar threads

Replies
16
Views
1K
Replies
18
Views
1K
Replies
15
Views
2K
Replies
4
Views
4K
Replies
107
Views
6K
Replies
7
Views
2K
Back
Top