Python Boolean and Conditional Programming

Besides numbers and strings, Python has several other types. One of them is Python’s boolean. Booleans, in combination with boolean operators, make it possible to create conditional programs: programs that decide to do different things, based on certain conditions.

What is a Boolean?

Let’s start with a definition:

A boolean is the simplest data type; it’s either True or False.

In computer science, booleans are used a lot. This has to do with how computers work internally. Many operations inside a computer come down to a simple “true or false.”

In Python, we use booleans in combination with conditional statements to control the flow of a program:

>>> door_is_locked = True
>>> if door_is_locked:
...     print("Mum, open the door!")
Mum, open the door!

First, we define a variable called door_is_locked and set it to True. Next, you’ll find an if-statement. This is a so-called conditional statement. It is followed by an expression that can evaluate to either True or False. If the expression evaluates to True, the block of code that follows is executed. If it evaluates to False, it is skipped. Go ahead and change door_is_locked to False to see what happens.

An if can be followed by an optional else:

>>> door_is_locked = False
>>> if door_is_locked:
...     print("Mum, open the door!")
... else:
...     print("Let's go inside")
Let's go inside

Thanks to our else-block, we can now print an alternative text if door_is_locked is False.

Python’s Boolean Operators

The ability to use conditions is what makes computers tick; they make your software smart and allow it to change its behavior based on external input. We’ve used True and False directly so far, but more expressions evaluate to either True or False. These expressions often include a so-called boolean operator.

Let’s look at these boolean operators in the REPL:

>>> 2 > 1
>>> 2 < 1
>>> 2 < 3 < 4 < 5 < 6
>>> 2 < 3 > 2
>>> 3 <= 3
>>> 3 >= 2
>>> 2 == 2
>>> 4 != 5
>>> 'a' == 'a'
>>> 'a' > 'b'

This is what all the boolean operators are called:

>greater than
<smaller than
>=greater than or equal to
<=smaller than or equal to
==is equal
!=is not equal
Python’s boolean operators

As can be seen in the examples, these operators work on strings too. Strings are compared in the order of the alphabet, with these added rules:

  • Uppercase letters are ‘smaller’ than lowercase letters, e.g.: ‘M’ < ‘m’
  • Digits are smaller than letters: ‘1’ < ‘a’

You’re probably wondering what the logic is behind these rules. Internally, each character has a number in a table. The position in this table determines the order. It’s as simple as that. See Unicode on Wikipedia to learn more about it if you’re interested.

Comparing different types in Python

When you try to compare different types, you’ll often get an error. Let’s say you want to compare an integer with a string:

>>> 1 < 'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'

This is how Python tells you it can’t compare integers to strings. But there are types that can mix and match. I would recommend against it because it makes your code hard to understand, but for sake of demonstration, let’s compare a boolean and an int:

>>> True == 1
>>> False == 0
>>> True + True
>>> False + False
>>> False + True
>>> True + 3

As can be seen, True has a value of 1, and False has a value of 0. This has to do with the internal representation of booleans in Python: they are a special kind of number in Python.

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!