How does this expression work in Python?
x=1, y=2, z=3
print(x and y and z)
Result is 3
If
x=3, y=2, z=1
print(x and y and z)
Result is 1
Anyone can explain this?
How does this expression work in Python?
x=1, y=2, z=3
print(x and y and z)
Result is 3
If
x=3, y=2, z=1
print(x and y and z)
Result is 1
Anyone can explain this?
or
operator in python returns first argument it is considered true, if not then it will return second. Neither value will be converted to boolean.
and
operator will return first argument if it is considered false, otherwise second. Once again no conversion.
so a or b
is a
if bool(a) == True
otherwise it's b
and a and b
is a
if bool(a) == False
otherwise it's b
EDIT:
edited my mistake - i switched and
and or
In the logical test, the returned value is the one that has been evaluated last.
so , in first test ==> print(1 and 2 and 3)
, the answer we get is 3
because it is last true value of the test.
In the second test ==> print(3 and 2 and 1)
, similarly the answer is 1
.