How do boolean operations work with parentheses in python 2.7? -


While playing around it found a slight inequality.

  gt; & Gt; 'Hello' == ('hello' or 'world') true & gt; & Gt; & Gt; 'Hello' == ('world' or 'hello') false & gt; & Gt; & Gt; 'Hello' == ('hello' and 'world') false & gt; & Gt; & Gt; 'Hello' == ('world' and 'hello') is true  

Is there some trick for this argument that I can not find it? Why is the order of the stars the reason for determining these questions? Should not I use all the brackets? Why does "and" flip output change?

Thanks a swindler

In Python, all objects can be considered "true" or "false". Evaluation of Boolean logic.

While doing this, Python uses this truth to create a shortcut. If it confronts a value that causes the argument to be "short-circuit" like or at the beginning of true , or incorrect At the beginning of the code> and , this simply gives certain values ​​ because it works because it assesses the value itself to be justified or correct , And therefore, whatever is being used, continues to work as expected. In fact, such operations always return the first value that allows them to fully evaluate the encounter (even if it is the final value).

  # "short-cycle" behavior & gt; & Gt; & Gt; 2 or 0 2 & gt; & Gt; & Gt; 0 and 2 0 # "normal" (fully evaluated) behavior & gt; & Gt; & Gt; 'Cat' and 'dogs' 'dog' & gt; & Gt; & Gt; 0 or 2 2  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -