Can anyone please explain me how does boolean logic works in python on numbers and strings ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

7th Jul 2021, 7:13 AM
Aditya Kumar
Aditya Kumar - avatar
4 Answers
+ 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
7th Jul 2021, 7:50 AM
visph
visph - avatar
+ 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)
7th Jul 2021, 7:42 AM
visph
visph - avatar
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.
7th Jul 2021, 7:44 AM
Elvish
Elvish - avatar
0
Visph answer is the best
7th Jul 2021, 8:05 AM
Pemadam