Kodeclik Blog

Boolean Operators in Python

Boolean operators in Python are among the most basic operators. Just like “+” is an arithmetic operator that takes numerical expressions and returns a numerical value, a boolean operator is one that takes boolean expressions (those that resolve to True or False) and returns another boolean expression.

The and Operator

Let us take the most basic boolean operator, the logical AND. AND returns True if and only if both of its operands are True. Here’s a simple Python program that prints the truth table for the boolean AND operator.
The output of this program is:
As you can see there are four combinations of truth values and the AND returns True only if both x and y are set to True.
You use the and operator, for instance, in an if conditional (or anywhere a conditional is used, e.g., a while loop).
The above program encapsulates the logic that, to obtain a learner permit to drive a car, you need to satisfy three conditions. First, you must be of more than 16 years of age. Second, you must pass a knowledge test. Third, you must pass a vision test. Only if you have all three conditions set to True, you are eligible to apply for a learner permit.
If you run this program, you will get:
If you update the first three lines to have False for even one of them the first “if” condition would not trigger and you will not be able to apply for a learner permit. The remaining lines will give you feedback on exactly what you are missing. For instance, if you set False, False, False as the values for the variables and run the program, you will get:
indicating that you need to satisfy three conditions before you are ready to apply for a learner permit.

The or Operator

The or operator returns True if any of its arguments are True. It is thus less strict than the and operator. Let us print out the truth table for the or operator:
The output is:
As you can see three rows have a True unlike the truth table for the and operator, which had True in only one row.
Notice that the or operator is less strict than the and operator. You use it for instance when you have multiple ways of satisfying a set of conditions. Consider the following program:
This program will print:
If you change any one of the two boolean variables to True, you will get:
As indicated by the above truth table, note that there are 3 sets of values for which you cannot play Tennis and only one set of values where you can play Tennis (namely when both conditions are False).
This means that the same program can be written as:
Note that this will give you the same result as before. The only difference is that now you are using the and operator instead of the or operator, This is called “De Morgan’s Law”.

The not Operator

Unlike the and and or operators which are binary operators (i.e., they take two arguments), not is a unary operator (it takes only one argument). It is used to negate the value of its argument. In other words, not applied to True gives False. Similarly, not applied to False gives True.
Here’s a program to print the truth table for not:
The output is:
Here’s a simple program to use the not operator:
The output, as expected, will be:
Hope you enjoyed learning about Python’s boolean operators! To learn more, checkout our blogpost on Python XOR.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.