Boolean logic | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Boolean logic

In Python I don’t understand how Boolean logic works. Could someone give me an explained example of Boolean logic in Python please?

12th Mar 2022, 9:22 PM
OnHonHon🤌🤌🍓
OnHonHon🤌🤌🍓 - avatar
1 Answer
+ 3
I like your demonstration code, FF9900! There is more to say about Python Boolean expressions when they involve values that are beyond merely True and False. The result is different from any other language that I know. Whereas other languages evaluate Boolean expressions to only True or False, Python returns whatever the last value was that determined the outcome. The order of operands matters. Swapping them gives different results: print(5 and 8) #result: 8 print(8 and 5) #result: 5 print(5 and 0) #result: 0 print(0 and 5) #result: 0 print(5 or 8) #result: 5 print(8 or 5) #result: 8 print(5 or 0) #result: 5 print(0 or 5) #result: 5 These outcomes can be surprising to someone who learned other languages first.
13th Mar 2022, 1:35 AM
Brian
Brian - avatar