Python and its weird boolean logic

I found some pretty interesting keywords in Python: and and or. Coming from a C background, this is somehow new to me since I’m used to using symbolic operators for boolean logic (&& and ||). One of the things I noticed was how Python interprets these keywords:

'a' and 'b'

It returns ‘b’. Surprised? I know I am. One look and I immediately thought it would return something similar to True since both values aren’t null.

A few more examples. I took them from here:

' ' and 'b'

This returns ‘ ‘. Apparently, and returns the first False value.

'a' and 'b' and 'c'

This returns ‘c’. If all values are true, and returns the last True value.

Aside from and, there’s another keyword that I mentioned earlier - or:

'a' or 'b'

If ‘a’ and ‘b’ returned ‘b’, this should return ‘a’, right? It does. I was just trying to mess with your head. or returns the first True value it encounters.

I’m not going to paste all of the examples so just read the rest here.

The reason why I find the keywords weird is because of their return values. They return the values compared instead of returning the usual True or False value. Well, of course Python has If-Else-statements as well, but I guess people needed another way of comparing values that they made those keywords to suit their needs.

comments powered by Disqus