In: Computer Science
What is the result of the following statement?
not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
It is python logical operator (not, and, or)
I know the result is 4 by running the code, but I don't understand the logic behind it, can you explain it step by step? e.g. How to eliminate those numbers in each step to get the result.
Thanks
Explanation:
1)AND operation example:
How 3 AND 4 is 4:
AND operator gives priority to the right value of the expression and gives that number as the result provided neither side should have 0 other wise 0 will be printed, for e.g
3 and 4 is 4 since on the right side of the expression the value is 4 and no side is 0, had it 0 anyside then the result of the expression would jave also been 0.
4 AND 3 is 3 since right side contains 3 and no value is 0
4000 AND 3 is 3 since right side contains 3 and no value is 0
0 AND 4000 is 0 since one side contains 0
2)OR operation example:
How 3 OR 4 is 3:
OR operator gives priority to the left value of the expression and gives that number as the result provided neither side should have 0 other wise if any side contains 0 then the other number will be printed .
3 OR 4 is 3 since left side contains 3 and no value is 0
0 OR 3 is 3 since left side contains 0 then the other number would be printed
1 OR 500 is 1 since left side priority, it contains 1 on left side and no value is 0
3)NOT operation example:
NOT operation for any positive value greater than 0 or any negative value would give false i.e 0 and for the value 0 it will give true i.e 1
NOT 0 is true
NOt 100 is false
NOTE: If any OR comes in a expression then the whole right and left side are considered as a single expression i.e the above rules apply for long expression also
Given expression:not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
not 1 or 0 is 0
not 1 or 0 and 1 is 0
not 1 or 0 and 1 or 3 is 3 since one side 0
not 1 or 0 and 1 or 3 and 4 is 4 since right side priority now all the expressions will be considred as two numbers as left and right since both and and or have already applied.
now, not 1 or 0 and 1 or 3 and 4 or 5 is 4 since left side priority, not 1 or 0 and 1 or 3 and 4 is considered as single number on left side, 5 is considered as single on right side
not 1 or 0 and 1 or 3 and 4 or 5 and 6 is also 4 since not 1 or 0 and 1 or 3 and 4 is one expression on left side , 5 and 6 is another single expression on right side
similarly for not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9, not 1 or 0 and 1 or 3 and 4 is one expression on left side , 5 and 6 or 7 and 8 and 9 is another single expression on right side.
Once one and and one or expression which is non-zero is achieved the total expression would then be considered as two one on left and one on right