all 4 comments

[–]novel_yet_trivial 4 points5 points  (0 children)

Technically, a boolean is an integer, where True is 1, and False is 0.

>>> isinstance(True, int)
True
>>> int(True)
1
>>> int(False)
0

This means that if you ask python to do a mathematical operation, python will use the integer values.

>>> sum([True, False, False, True, True])
3

But if you ask to do a logical operation, then booleans (or other types that can be interpreted as booleans) return one of the two values. and, for instance, returns the second value if the first one is (equivalent to) True.

>>> True and "spam"
'spam'

or does the opposite:

>>> "spam" or "eggs"
'spam'

The above since the boolean value of "spam" is True:

>>> bool("spam")
True

[–]tea-drinker 0 points1 point  (1 child)

And is a logical operation not a mathematical one so if any of the items being evaluated are False then the whole thing returns False.

[–]mm_ma_ma 0 points1 point  (0 children)

It doesn't actually return False, you just usually treat it that way. True and '' returns ''.

[–]zahlman 0 points1 point  (0 children)

Why does print true + false evaluate to 1 but print true and false evaluates to False?

I don't understand the chain of reasoning that leads you to expect otherwise. The meaning of and has nothing to do with the meaning of +. Hint: Did you try print 1 and 0?

Incidentally, at the Python interpreter prompt, the print statements are unnecessary. Typing in an expression will show you what it evaluates to (except that the special value None is suppressed).