Explain how the OR and AND works for following line of codes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

16th May 2019, 8:22 PM
Shivam Tewari
Shivam Tewari - avatar
11 Answers
+ 4
Vasiliy 2 is also true and not stop
17th May 2019, 3:03 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 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
16th May 2019, 8:35 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 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?
16th May 2019, 8:40 PM
HonFu
HonFu - avatar
+ 3
Shivam Tewari 0 is false and any other int is true
16th May 2019, 9:09 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
Shivam Tewari thats what HonFu was saying
17th May 2019, 9:17 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 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.
16th May 2019, 9:12 PM
HonFu
HonFu - avatar
+ 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
16th May 2019, 9:02 PM
Shivam Tewari
Shivam Tewari - avatar
+ 1
(c = 0 or 2) == (c = false or true) -> (c = true) -> c = 2 (c = 1 or 2) -> (c = true, stop) -> c = 1
16th May 2019, 9:17 PM
Solo
Solo - avatar
+ 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
17th May 2019, 9:06 AM
Shivam Tewari
Shivam Tewari - avatar
+ 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);
17th May 2019, 9:24 AM
Solo
Solo - avatar
0
*AsterisK* Explain, I did not understand.
17th May 2019, 9:01 AM
Solo
Solo - avatar