Operator Precedence | SoloLearn: Learn to code for FREE!


Operator Precedence

Hi everyone, I tried a command below and its output was 7 7 == 5 or 7 I am confused about it, In my view 7==5 will become False and False or 7 should lead a "True" output But it's 7 Please guide me why, thanks a lot

11/20/2016 11:22:58 AM

Zachary S. Chao

1 Answer

New Answer


This is because Python is lazy. When you use a logical operator like 'and' or 'or', Python computes the beginning of the operation first. In your case: '7 == 5'. This, as you said, evaluates to false. So now Python, lazily, thinks: "If the next part is true then the whole result is true, and if the next part is false then the whole result is false. I'll just return the last part, whatever it is." That's why the final result is 7.