When evaluating conditions using if statements, it is often helpful to be able to do multiple comparisons at one time, to limit the number of if statements used in our code. This will allow us to write cleaner and more concise code, that is easier to read. Logical operators, or Boolean operators, are logical tools that will allow us to combine conditions together.
To start, we will take a look at what comparisons we are able to do in Python, and what actually happens when we do these comparisons. In Python, there are six comparison operators that are typically seen in if statements. These comparison operators are:
1. Less than (<)
2. Less than or equal (<=)
3. Greater than (>)
4. Greater than or equal (>=)
5. Equal (==)
6. Does not equal (!=)
When we use one of these operators, the result can be one of two values. It will either be true if the condition is satisfied, or false otherwise. If we want to do multiple comparisons at one time, we need to understand how our computer interprets true and false values. When our computer encounters true and false, it assigns them numeric values of 1 and 0 respectively. From here, our computer applies a set of logic based on math, known as Boolean Algebra. Boolean Algebra sets out the rules of what results to expect when combining comparison operators. It is heavily used in electronics, circuits, and it is the basis of modern computation. Boolean Algebra contains a lot of theorems and ideas that are useful for working with binary numbers, however for this article we will focus on three that are most commonly used.
The first operation we will discuss is know as and. The and operator allows us to check if two or more conditions are true at the same time. To understand this, let’s take a look at a simple example. Suppose a user inputs a country and age, and we want to determine if they inputted a country “Canada” and an age 18. If we were to use just if statements to solve this problem, we would get the code below.
You can see that this code contains a fair bit of repetition and checking. When we validate each condition separately, we end up with longer code that is harder to read. Using the and operator, we can combine the two checks into one single check to simplify the code.
Using the and operator results in shorter code, and only one if statement. This means that our code is not only easier to read, but also more efficient. To understand why this code is equivalent to the code without the and operator, we can chart the results of using and based on the result of the condition.
From this table, you can see that the only cases where the and operator gives a true result is when both of the conditions being checked gives a true result. It is important to note that this rule is true for more than two conditions as well.
The or operator allows us to check if at least one condition is true. If at least one of the conditions we are checking is true, then the result of the or operator will be true. Let’s look at another example to see exactly how this works. Suppose we ask the user to input the weather, and we want to decide if they should wear a jacket. We will output that the user should wear a jacket if the temperature is below 10 degrees, or if it is raining. If we use just if statements to solve this problem, we would get the code below.
Similar to the and operation example, you can see that this situation leads to code repetition, which is not ideal. Instead of having an else if, we can combine the two checks into a single condition, using the or operator.
The or operator allows us to remove one of our condition checks, again resulting in shorter and more efficient code. In general, if we have two conditions, we can outline the results of the or operator.
As you can see, the only time or is false is if all of the conditions involved are false. Like and, the or operator will have the same result with more than two conditions as well.
The final operation that I will discuss is the not operator. The not operator simply negates the result of the current condition. This means that if the operation gave a true result, the not of the operation would be false. If the result were false, the not would make it true. The not operator is helpful in situations where it is easier to check for the opposite operation and negate the result. For example, suppose we wanted to ask the user to input a forecast for the weather and determine if they did not enter “rain”. We can do this with a not operator as follows.
In general, the outcome of the not statement acts on one condition, as follows.
These three operators are some of the basic logical operators that exist in Python. They are used frequently, as they allow for clean and efficient code. By understanding these operators, you will be able to solve many complex problems in simple and elegant ways.