3 Curiosities of Python Boolean
Better understanding Python boolean through three edge cases

Plenty of other excellent articles have described the consequences of mixing up equality (‘==’) and identity (‘is’). But there are plenty of bizarre syntactic edge cases that deserve appreciation!
1. ‘True is False is False’…. is ‘False’
Multi-boolean statements are tempting to read left to right. ‘True is False’ is false, so if parsed in order one might assume that ‘True is False is False’ is ‘True’. This is the case for ‘(True is False) is False’, but without explicit grouping, all terms are evaluated for identity. Similarly, the statement ‘False is False is False’ is ‘True’ as all three operands are the same.
2. ‘“Cat” and True is True’… but ‘True and “Cat”’ is “Cat”’
In Python, objects can be assessed for truth value. Often, this serves as syntactic sugar to avoid more verbose checks of variables that may or may not be assigned at runtime.
Typically, the consequences of Python's “truthiness” are obvious. To quote from the documentation:
By default, an object is considered true unless its class defines either a
__bool__()
method that returnsFalse
or a__len__()
method that returns zero, when called with the object.Here are most of the built-in objects considered false: constants defined to be false:
None
andFalse
; zero of any numeric type:0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
; empty sequences and collections:''
,()
,[]
,{}
,set()
,range(0)
The nastiness comes when logical statements are chained. It is easy to lose track of when the returned value of a statement will be a boolean (True or False) or the original argument value.
Counter-intuitively (at least to me!) the returned value is that of the last evaluated statement (make sure to think about what this means with ‘or’ which will stop evaluating at the first truthy True!). In the examples below, the outcomes are bizarre but would likely be easy to catch in context.
The more insidious bugs sneak into conditional branching logic…
3. ‘{True: “a”, 1: “b”, 1.0: “c”}’… is ‘{True: “c”}’
Dan Bader brilliantly captured this Python riddle (see the original article for additional CPython nuances!), and it brings to light an important equality consideration when working with Python booleans.
Python holds equality between True, 1, and 1.0.
This counter-intuitive dictionary construction demonstrates that the Python interpreter treats all three as equal. As it constructs the dictionary, it first adds a new key ‘True’ with value ‘a’. When assigning a value for the second key ‘1’, it finds that the key is already in the dictionary (‘True’ is in there, and they are equal!), so it updates the value. Again, it finds that ‘1.0’ is already in the dictionary as a key (still ‘True’) and updates it to the final value ‘c’!
Leave a comment or a clap if you’ve learned something!
More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.