Python Boolean Logic: Not As Scary As It Sounds

Python Boolean Logic: Not As Scary As It Sounds

True or False? You use some combination of these opposite pairs everyday: Yes. No. Up. Down. Left. Right. Forward. Back. On. Off. If your answer is True, then you’re already a computer scientist! Why stay inside learning Boolean logic? You can stop reading now and go outside and play.

Boolean Logic

What? You’re still reading? You want to know more? Okay. The above examples are called binaries. The concept of binary goes way back to Gottfried Leibniz. In addition to wearing crazy wigs, Leibniz developed a system of formal logic that used 1s and 0s to perform arithmetic. This was the foundation of binary as we know it today. In the mid-19th century, mathematician George Boole developed a system of algebra based on binary, using True and False and three basic operators: AND, OR and NOT. Today we refer to it as Boolean logic or Boolean algebra. Boolean logic is the foundation of digital circuits. In a nutshell (no pun intended), your computer is a very complicated arrangement of a very simple idea. The switch is on or the switch is off. The digit is equal to 1 or the digit is equal to 0. The value is True or the value is False. From these basic elements we can build complex programs.

Computer scientists refer to Boolean operators as Booleans, for short. As I’m sure you guessed, they are a core data type in Python. The interpreter refers to them with the keyword ‘bool’. Try this at the prompt:

Boolean Operators

We can produce Booleans without assigning them to variables, but instead make comparisons and evaluations using operators. We can work with the following:

  • == (is equal to)
  • != (is not equal to)
  • and
  • or
  • not
  • >= (is greater than or equal to)
  • <= (is less than or equal to)


Let’s start with ==. Check this:

Now try:

Did you expect False? Python 3 equates 0 with False and 1 with True. Even though 0, on its own, evaluates to False, the comparison is True because, despite popular opinion, 0 is in fact equal to 0. The following are also valid expressions:


Now let’s look at !=. It looks weird, but it’s very straightforward. It’s like somebody shouting to get your attention, “Hey! This is not equal to that!”

In the example above, “It’s True! One is not equal to zero!” and “False! One is not equal to one? Are you kidding me?!”


I said earlier that you are already a computer scientist, and it’s true: you use Boolean logic all day, every day, when making decisions and answering questions. We can assign Boolean values to variables.

The comparison between cats and cats returns True because it is true, cats and cats are the same, while dogs are a different, albeit superior, beast.


The or operator lets us compare one or more values and returns True only if at least one of the values is true. It will return False if none of the values are True.


The not operator negates the value we apply it to, returning the opposite. For example:

This is like saying, “If something is not True, it is False. And if something is not False, it is True”:

>, <, >=, <=

While these symbols are great for emoticons, they are even more powerful when used mathematically. Try the following at the Python prompt:

This is the greater than symbol. We evaluated whether or not one is greater than zero, which is obviously True. If we reverse the operator, we evaluate whether or not 1 is less than 0:

Like before, we can assign values to variables and evaluate:

As you can see, dogs are greater than cats. We can also evaluate whether or not a value is greater than OR equal to another value.

That last example is tricky, it appears as though it should return False, but it’s True. One is greater than or equal to False.

This is all very well and interesting, but comparing apples to oranges gets old real quick. What’s the point? In our next chapter, we will introduce if Statements, which are >= Boolean logic.

Python Boolean Logic: Not As Scary As It Sounds was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid