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:
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.