+ 1
How this is 2?
Code python: a = 0 or 1 b = 0 or 2 c = a or b print(a + b + c) ______________________________________ Output: 4
4 Answers
+ 3
Usama Nadeem đ”đ°, let's go over the code line by line and understand something about "OR" logic operators. The OR operator returns True if any of its operands is True. When evaluating an expression such as: x or y, if x is True (or a number other than zero (0)), the expression returns True (or returns the value of x) and doesn't bother to check the value of y.
In the line: a = 0 or 1, python checks the first operand and since it is 0 (False), it checks the second. Since the second is non - zero (True), the value 1 is assigned to a.
In the next line: b = 0 or 2, the value 2 is assigned to b.
In the third line: c = a or b, the interpreter checks the first operand (a), since a is 1, a non zero (True) value, there is no need to check the second operand and the value of 1 is assigned to c.
a (1) + b(2) + c(1) = 4.
Understand?
+ 1
hmm, I just tested in playground. shows me 4
https://code.sololearn.com/ca19a0a21254
+ 1
David Akhihiero Thank you for your precious time I understood it.âŁïž
0
But how ?