The ABCs of Python: “False,” “True,” Stephen Colbert and Santa Claus

It could reasonably be argued that the two most important keywords in Python are False and True. As we noted in Are Booleans from Outer Space?, almost everything in Python  boils down to these concepts. Let’s start with the latter.

False

False means, as you would expect, “not true.” But that doesn’t quite state the immense importance of False (and, yes, Python wants the word capped). Python has a deeply encoded instinct for falsehoods. Just as Santa Claus knows when you’ve been good or bad, Python knows when you’ve been True or False (at least within confines of basic mathematics).

Python is not, however, strictly mathematical in its reasoning. It has other opinions about False that are only logical from its own computer program perspective. For example, it views the number 0 as False and the number 1 as True. Although this sounds odd, it is logical from a Boolean perspective and turns out to be immensely useful in certain programs.

The following code, when put into your IDLE shell, gives you a taste of how False works:

>>> 7 == 8
False
>>> 9 > 10
False
>>>False == 0
True
>>>False == []
True
>>>[] is []
False

True

This keyword is, of course, the other side of the same coin. Not only does Python tell you when basic mathematical assertions are True, it’ll also perceive some other circumstances as True. The following code gives a sense of how it works:

>>> 5 * 5 == 25
True
>>> 5 + 5 == 10
True
>>> True == 1
True
>>> square = 4
>>> square == 4
True
>>> square == 3
False
[] == []
True

Stephen Colbert

As the former incarnation of Stephen Colbert might have said, that there’s just not enough “truthiness” in Python. You can’t claim something is True just become because it “feels right.” In a way, Python is your anti-Colbert, always basing its arguments on rigorous logic. On the other hand, it’s quite possible to get your computer to tell falsehoods, as long as you correctly evaluate its underlying, internal logic. In this, perhaps, maybe all computers can demonstrate an element of truthiness of which Colbert would be proud.

Featured image: 1881 illustration by Thomas Nast 

Stephen Colbert photo from Montclair Film, https://www.flickr.com/photos/montclairfilmfest/49205031397/

Are Booleans from Outer Space? 

On the indispensable nature of Booleans in Python

There are a couple of Python operators worth spending a bit of extra time on. These are the Booleans, which sound to me like some species of Star Trek alien. But they are from inner rather than outer space. That is, to the degree your computer has internal thoughts, it thinks in Boolean.

Computers are, of course, made up of a multitude of circuits, each one of which represents a binary digit of information (aka, a bit). When one of these circuits is on, it represents a “1”, and when it’s off, it represents a “0”. This dichotomous thinking is also represented by Boolean truth values, which come in just two bool types: True and False.

Try this in your IDLE Shell. Type

>>>4 == 5

Hit Enter. The program should print the word False in return. Now do the same with:

>>>6 > 5

This time you get the word True. Clearly, the program knows what’s true and what’s not at a basic mathematical level. That comes in very handy.

Boolean truth values are based on Boolean logic, which was created by a one of those crazy smart English dons of the early Victorian Age. At its most basic level, it’s pretty easy to explain:

George Boole
Wikipedia

You start off with a statement that is either True or False. There’s no messy middle ground here. Then, you add in other statements, all of which are also either True or False. You combine those statements by using some Boolean operators (which are “and,” “or” and “not”) and, presto, you’ve got a computer program!

Let’s say, for example, you want to automate a dog catcher. The dog-catcher robot is tooling around looking for stray dogs. It sees some animal crossing the street. One line in its code determines whether or not it is a dog. If the animal is a cat, for example, the line of code delivers False. But if it’s a dog, it delivers True.

Now another line of code asks whether or not the dog is loose. If the dog is on a leash, then this line of code returns False, but if the dog is walking by itself with no leash, then the code returns True.

Here you have two true statements that will result in a certain action: one truth is that the animal is a dog, and the other truth is that it’s loose. When both these statements are True, the automated dog catcher goes ahead and chases down the dog. (Yes, this sounds like a horrible tech to me, too).

Sure, writing a dog-catching program is not quite that easy, but you get the point. Basically, everything in your code boils down to True and False statements.

These statements follow a specific logic when you combine them. That logic can hurt your brain a bit when you first start delving into it, but ultimately you (or, at least, I) start to see it as a thing of beauty. In fact, I prefer to see the following “rules”—which I took from Codecademy—as a kind of poem. Read it aloud and you might see what I mean:

The Boolean Operators at Work

True and True is True
True and False is False
False and True is False
False and False is False
True or True is True
True or False is True
False or True is True
False or False is False
Not True is False
Not False is True

Maybe someday you’ll want to memorize these basic Boolean rules, but for now it’s enough to just admire them. I should note that when you write something as simple as “6 > 5” (as we did above), it’s what’s known as a “condition.” In this case, the condition is “True”.

There are lots of ways of using conditional statements. In fact, you can weave them together in a virtually infinite number of ways.

PS – By the way, a really nice explanation of Boolean logic can be found at I PROGRAMMER.

Featured image from Joe Wos. Wikimedia Commons. 25 October 2020.