Why in the world does Python do this?

As seen in Idle and Python 3.5.2

>>> print(True or "true")
True
>>> print(True and "true")
true
>>> print(True and "false")
false

Note the difference in case.

>>>. a=True or "true"
>>> a
True
>>> b=True and "true"
>>> b
'true'
>>> c=True and "false"
>>> c
'false'
>>> type(a)
<class 'bool'>
>>> type(b)
<class 'str'>
>>> type(c)
<class 'str'>

http://www.diveintopython.net/power_of_introspection/and_or.html

2 Likes

The first set use str and the second set use repr

>>> print(str(True or "true"))
True
>>> print(str(True and "true"))
true
>>> print(str(True and "false"))
false
>>> a=True or "true"
>>> print(repr(a))
True
>>> b=True and "true"
>>> print(repr(b))
'true'
>>> c=True and "false"
>>> print(repr(c))
'false'
>>> type(a)
<class 'bool'>
>>> type(b)
<class 'str'>
>>> type(c)
<class 'str'>

Or, did I misunderstand the question?

Wow, just when I thought I knew a language…

boo hisssssssssssssss.

I think…

All strings on the right are simply strings and will evaluate as True.

In case a the or operand was satisfied with True, and that was what was returned. Won’t even get to dealing with "true".

In b and c the first part of and is processed, passes, and onto the second part. The string also evaluates as True and being where the line leaves off, it’s the value returned.

That about right? Now I go read the link…

1 Like