0
Can anyone please explain me how does boolean logic works in python on numbers and strings ?
Example - 45 and 56 gives 56 'abc' and 'cba' gives 'cba'. Give a common explanation for all the questions of this types.
4 odpowiedzi
+ 2
v1 and v2 return v1 if v1 is False as v1 and v2 would be False with any value in v2, and return v2 otherwise
v1 or v2 return v1 if v1 is True as v1 or v2 would be False with any value in v2, and return v2 otherwise
+ 1
operand of boolean logic operator are evaluated to True or False, and last real value evaluated is returned from the whole expression:
if 'and' first False value or last True value is returned (no need to check further if False encountered)
if 'or' first True value or last False value is returned (same but converse logic)
0
I tried it myself and found that
a and b = b
a or b = a
no matter what a, b are (except 0).
But still I cannot explain why it could happen in a binary way, and how it can be used.
0
Visph answer is the best