What is truth? - part 2 - true vs one
15 July 2007
So following on from part one, we have True and False in Python, which are somewhat equivalent to the Boolean 1 and 0:
> "The two-element Boolean algebra is the simplest Boolean algebra, ... having just two elements, named 1 and 0 by convention." Source
So we have 1 and 0, True and False, you can think of a light switch that has two states, on and off.
This gives us four sums:
0 + 0 = 0 i.e. two wrongs don't make a right.
0 + 1 = 1 i.e. a right is not negated by a wrong, a lie cannot negate the truth.
1 + 0 = 1
1 + 1 = 1 i.e. a pair of rights are right, a pair of truths are true.
Now look at the following interactive Python session:
> >>> False + False == True > > False # two wrongs do not make a right. > > >>> False + True == True > > True # a right is not negated by a wrong > > >>> True + False == True > > True # a right is not negated by a wrong
Makes sense so far, but lastly:
> >>> True + True == True > > False # two rights make a wrong!
This is of course opposite to what we, at first, might expect.
What's going on here?
Well in Python, unlike some other dynamic languages, the arithmetic operators are separate from the Boolean comparison operators:
> >>> True + True == False > > True > > >>> (True and True) == True > > True
Another thing that gets everyone once is that the assignment operator (=) is different from the comparison operator (==). Anyway, to get the complete Boolean behaviour, you need to write the operators out in words, i.e. and, or, not, etc.
Otherwise, when using the arithmetic operators, Python converts the Boolean values (true false) into integers (1 0) when it interprets the code:
> >>> True + True == 2 * True > > True > > >>> (True and True) == 2 * True > > False > > >>> False + False == 2 * False > > True > > >>> (False and False) == 2 * False > > True



