+ 1
why the below statement is not True ?
x=4 y=2 if not 1+1==y or x==4 and 7==8 in my sight the last part (7==8) is False so because of 'and' operator the whole statement should be false then 'Not' make it True which part i am making mistake ?
6 ответов
+ 1
i find my problem but still i dont know why
in my mind i calculate two sides of 'Or' statement first(which is True) then affect it by 'And' (which is false) then affecting 'Not' .like this :
if not ( (1+1==y or x==4) and 7==8)
but it seems python read it in a different way
from first to last or last to first !
+ 1
Like mathematical operators, boolean operators are evaluated in a certain order.
not > and > or
Left side of 'or':
not (1+1 == y)
not (2 == y)
not (true)
resulting in 'false'.
Right side of 'or':
x == 4 and 7 == 8
true and false
resulting in 'false'
Finally:
false or false = false.
Note that, mathematical operators have higher precedence than boolean operators. Thus (1+1 == y) is evaluated before not, on the left side of the or.
0
the and always needs both sides to be true but the right side is already false so change that or remove not from the condition
0
Do the equalities first and the statement becomes:
not true or true and false
Not is evaluated next:
False or true and false
Then applying the operators from left to right and noting that false or true is true and trust and false is false, we get the answer as false.
0
not true or true and false
false or true and false
true and false
false
precedence order is : not > or > and but after operations of equality or comparison operators
- 2
if not 1+1==y or x==4 and 7==8
not false or true and false
true or true and false
true and false
false
..
just my thought..