A long list of checks scrolls down the launch screen: fuel, weather, crew, doors, cables. Each check ends with a green light or a red light, nothing in between.
"Every check is a question," Rocket says, tapping the screen. "And every answer is yes or no. I want my programs to ask questions like that."
Raven reads the first few checks aloud. "What do you notice?" she asks. "Each one compares two things. Fuel compared to eighty. Wind compared to a limit."
Nova flashes green, then red. "I found a pattern," she says. "In Python, a comparison is a question. The answer is always True or False."
Rocket opens the shell to start asking.
| Operator | Means | Example | Answer |
|---|---|---|---|
| == | is equal to | 5 == 5 | True |
| != | is not equal to | 5 != 3 | True |
| < | is less than | 2 < 1 | False |
| > | is greater than | 9 > 4 | True |
| <= | is less than or equal to | 3 <= 3 | True |
| >= | is greater than or equal to | 7 >= 8 | False |
Careful: one = stores a value, and two == ask a question. The line fuel = 40 stores 40. The line fuel == 40 asks whether fuel holds 40.
The answers True and False are booleans, the bool type you met last week. They start with a capital letter.
>>> fuel = 40 >>> fuel >= 50 False >>> fuel != 0 True >>> "go" == "Go" False >>> "go" == "go" True
Strings are compared letter by letter, and capital letters count. So "go" and "Go" are not equal.
| fuel = 40. The comparison | True or False? |
|---|---|
| fuel > 40 | ? |
| fuel >= 40 | ? |
| fuel == 40 | ? |
| fuel < 10 | ? |
| fuel != 50 | ? |
A comparison gives back a value, so you can store it in a variable like any other value. Its type is bool.
Good names for these variables read like a yes or no question, such as fuel_ok or crew_ready.
# checks.py
fuel = 75
wind = 12
fuel_ok = fuel >= 80
wind_ok = wind < 20
print("Fuel OK:", fuel_ok)
print("Wind OK:", wind_ok)
print(type(fuel_ok)) Fuel OK: False Wind OK: True <class 'bool'>
| Statement | True or false? |
|---|---|
| ready = fuel >= 80 stores True or False in ready. | ? |
| true with a small t is a bool value in Python. | ? |
| 5 != 5 gives False. | ? |
| "Nova" == "Nova " gives True. | ? |
Every decision a program makes starts with a True or False question. Tomorrow you will use those answers with if and else.