+ 1
Explain how the OR and AND works for following line of codes?
a = 1 or 0 b = 1 or 1 c = 0 or 2 print("a=" + a) print("b="+b) print("c="+c) a1 = 1 and 0 b1 = 1 and 1 c1 = 0 and 2 print("a1="+a1) print("b1="+b1) print("c1="+c1) Actually I know concept of AND and OR, but for code above can you explain that how 'c=0 or 2' assigns c=2
11 Answers
+ 4
Vasiliy 2 is also true and not stop
+ 3
If you want to know how AND and OR works you can read it on SoloLearn, just use the search bar, that way you will be able to understand better and use it to answer this, which will probably help you grow too
+ 3
Both 'and' and 'or' continue to check until the answer is found, and both return the last checked value.
'and' needs all values to be True, 'or' needs one value to be True.
So 'and' returns the first False value (because it's already decided: False) or the last value, 'or' returns the first True value (because it's already decided: True) or the last one.
Can you figure it out like this?
+ 3
Shivam Tewari 0 is false and any other int is true
+ 3
Shivam Tewari thats what HonFu was saying
+ 2
If you apply what I wrote, you get there. Let me demonstrate.
0 is False, but 'or' wants to find at least one True value. 2 is True, so 'or' stops the evaluation and the value becomes 2.
If it was 'and', result would be 0.
If first value was 1, result would be 1.
Please try to apply the logic to all the other cases.
+ 1
Actually I know concept of AND and OR, but for code above can you explain that how 'c=0 or 2' assigns c=2
+ 1
(c = 0 or 2) == (c = false or true) -> (c = true) -> c = 2
(c = 1 or 2) -> (c = true, stop) -> c = 1
+ 1
I found the below explanation best answering my code above :- by, MARIAM CHAKHVADZE
When we use logical and, If both the operands are true then condition becomes true.
For example, 1 and 4
Its true but when we write x = 1 and 4
x is 4
Because when we use logical and, it checks the first operand and when sees its true than checks second one,and when sees its true,than becomes it.
For example, x = 0 and 1
x = 0
Because it checks the first operand and sees that is false. So it doesnt care about second one and x become 0
So for logical or, If any of the two operands are true then condition becomes true.
It means that, if we have x = 0 or 1
x = 1
And if we have x = 4 or 0
x=4
+ 1
Shivam Tewari
I would explain it easier:
"or" - discovers "true" and stops;
"and" - discovers "false" and stops.
or:
"false" = 0; "true" =1;
"or" -> 0+1;
"and" -> 0*1, or 1*(i>=1);
0
*AsterisK*
Explain, I did not understand.