On Fri, 14 Jun 2013 10:29:25 -0600, Michael Torrie wrote: > On 06/14/2013 03:50 AM, Nick the Gr33k wrote: >> >>> print(name or month or year) >> abcd >> >>> print(name and month and year) >> ijkl > > Interesting. I'd have thought a boolean expression would return True or > False, not a string. Learn something new every day.
Correct. In Python, all boolean expressions are duck-typed: they aren't restricted to True and False, but to any "true-ish" and "false-ish" value, or as the Javascript people call them, truthy and falsey values. Unlike Javascript though, Python's idea of truthy and falsey is actually quite consistent: Truthy: - True - any non-zero number - any non-empty string - any non-empty list/tuple/dict/set etc. - object() - anything else best considered as "something" Falsey: - False - zero - empty string - empty list/tuple/dict/set etc. - None - anything else best considered as "nothing" There are a couple of anomalies -- the timestamp representing midnight is falsey, because it is implemented as a zero number of seconds; also exhausted iterators and generators ought to be considered falsey, since they are empty, but because they don't know they are empty until called, they are actually treated as truthy. But otherwise, the model is very clean. -- Steven -- http://mail.python.org/mailman/listinfo/python-list