+ 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 ?

28th Jul 2016, 4:50 PM
me3
6 Respostas
+ 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 !
28th Jul 2016, 5:28 PM
me3
+ 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.
28th Jul 2016, 6:47 PM
Hasan Sajid
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
28th Jul 2016, 4:59 PM
kaleab
kaleab - avatar
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.
28th Jul 2016, 10:29 PM
Andrew Rose
Andrew Rose - avatar
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
11th Oct 2016, 11:06 AM
Suraj Kumar
Suraj Kumar - avatar
- 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..
29th Jul 2016, 9:14 PM
Sann Htun Naing
Sann Htun Naing - avatar