When starting out in programming, the idea of a boolean variable, something that is either just true or false, seems like an overly simple thing… something that feels rather useless.
In fact, booleans in computer code are everywhere. They are simple, but also useful in many ways. You’ve probably heard about how everything in computers is ones and zeros at the lowest level - and that’s true. But on this super simple base of 0 and 1 is built all of the power of the internet, and all the apps you’ve ever used.
When you are coding, you often have to make a choice about what to do next based on some kind of condition or to do something repetitively (over and over) based on some condition. Something like is there gas in the car? or are we moving faster than 100mph? In real life, these are considered to be YES or NO kinds of questions. If the gas tank is empty, the question results in a FALSE condition. If there is some gas in the tank, then the question’s result is TRUE, "yes, there is some gas in the tank."
And while this may seem super simple to you, and it is, it is also very powerful when used in a program.
This idea of a condition that is either TRUE or FALSE, is known as a boolean expression. And in Python, they crop up everywhere. They are in conditional statements and they are part of loops.
Boolean expressions can be very complex, or very simple:
playerOne.isAlive() == true
might be a key thing to know inside of a game. But it might be more complicated:
player[1].isAlive() == true and player[2].isAlive() == true and spaceStation.hasAir() == true
All three things need to be true to continue the game. Using boolean expressions, we can build very powerful tests to make sure everything is just as we need it to be.
We also need more kinds of boolean expressions when we are programming. Things like less than or greater than or equal to, and other comparison operators so we can compare things to work out the relationships within our data.
healthScore = 5We need a way to ask about expressions like "is healthScore less than 7?? (very healthy)" or "is healthScore greater than or equal to 3?? (maybe barely alive?)"
To do that we need a bunch of comparison operators.
| Operator | Description | Example |
|---|---|---|
== |
Equal to |
x == 5 |
!= |
Not equal to |
x != 55 |
> |
Greater than |
x > 1 |
< |
Less than |
x < 10 |
>= |
Greater than or equal to |
x >= 5 |
<= |
Less than or equal to |
x <= 5 |
Each of these can be used to make it very clear to someone reading your code what you meant. Imagine a flight simulator, where you’re flying a big, old fashioned airplane. The code that keeps track of the status of the plane might need to be able to make decisions on boolean expressions like:
altitude > 500.0 # high enough to not hit any trees!
airspeed >= 85.0 # fast enough to stay in the air.
fuelAvailable <= 5.0 # need to land to refuel!
totalCargoWeight < 6.0 # more than 6 tons and we can't take off!
pilot.isAlive() and copilot.isAlive() # everything is fine, keep flying.Like the ANDs in the examples above or this last boolean expression with the and` in it, we have in Python the ability to combine expressions into larger more complex expressions using and and or.
The logical operators are AND and OR, except in Python we use and for AND and or for OR.
| Operator | Description | |
|---|---|---|
and |
Logical AND |
playerOneStatus == 'alive' and spacecraft.hasAir() |
or |
Logical OR |
room.Temp > 70 or room.Temp < 75 |
The and operator is an operator where BOTH sides have to be true for the expression to be true.
(5 < 9) and (6-3 == 3) # trueSee how both expressions on either side of the and are true? That makes the entire line true.
The or operator is an operator where if ONE or the OTHER or BOTH boolean expressions are true, the entire expression is true.
(5 < 9) or (6-3 == 3) # true
(5 == 4) or (7 > 3) # true!
(5 == 4) or (6 == 2) # false (both are false)Both sides of a logical operator need to be Boolean expressions. So it’s all right to use lots of different comparisons, and combine them with and and or.
# deep in a cash machine application...
(customer.balance() <= 20.00 \
and \
customer.hasOverDraftProtection() == true) \
or \
(customer.savings.balance() > 20.0 \
and \
customer.canTransferFromSavings() )See how these conditions could line up to allow a customer to get cash from the cash machine? Again, this is why boolean expressions are important and powerful and why coders need to be able to use them to get the software just right.
|
Tip
|
|
Here is an example:
houseTemp = 67.0
thermostatSetting = 70.0
print(houseTemp >= 55.0)
print(houseTemp <= thermostatSetting)
print(thermostatSetting != 72.0)
print(houseTemp > 65.0 and thermostatSetting == 68.0)These print statements should produce True, True, True and False.