Operator Precedence | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

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

20th Nov 2016, 11:22 AM
Zachary S. Chao
Zachary S. Chao - avatar
1 ответ
+ 4
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.
20th Nov 2016, 3:35 AM
Arthur Busser
Arthur Busser - avatar