+ 3

What happens when I compare Boolean value with string value using Boolean operator?

True and 'spam' == spam? I searched for Boolean operator and what I got was: 'and is Boolean operator. This compares the two value and evaluates to single Boolean value.' First, I wondered why it doesn't evaluate to single Boolean value. So I thought maybe 'True and' doesn't affect anything. But when I tried True or 'spam', It turned out to True and I was wrong! I searched it on web but there was no Python. Here's more thing I experimented. https://code.sololearn.com/c7dHDGfw1ywZ/?ref=app

22nd Apr 2017, 3:59 AM
Joel Bennett
Joel Bennett - avatar
3 Réponses
+ 2
x AND y (if x is false, returns x, otherwise y) x OR y (if x is true, returns x, otherwise y) Here you can find more info: https://docs.python.org/2/reference/expressions.html#boolean-operations Note that: bool("") returns False bool("any non-empty string") returns True
22nd Apr 2017, 11:28 AM
SamG
+ 4
Now I understand the algorithm of Boolean operator..! I should have researched more about it! Thanks for giving me the detailed answer and make me think about the basic principles of Boolean operator! Have a nice day!
22nd Apr 2017, 11:57 AM
Joel Bennett
Joel Bennett - avatar
0
You're welcome! Have a nice day, too.
22nd Apr 2017, 11:58 AM
SamG