← Back to course
Code Crew G6+ / Week 03 / Monday
1/6
Week 03 Β· Decisions

Monday

True or False?
// if, elif, else and the logic words
⏱ about 30 min

Monday: True or False?

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.

Comparison operators

OperatorMeansExampleAnswer
==is equal to5 == 5True
!=is not equal to5 != 3True
<is less than2 < 1False
>is greater than9 > 4True
<=is less than or equal to3 <= 3True
>=is greater than or equal to7 >= 8False

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 comparisonTrue or False?
fuel > 40?
fuel >= 40?
fuel == 40?
fuel < 10?
fuel != 50?
WHY THIS EXERCISEA comparison is a question about the value right now, and its answer is True or False.
ONE EQUALS SIGN OR TWO?
  • Read the question.
  • Tap your answer.
Which line stores 3 in crew?
Which line asks whether crew holds 3?
Predict: what does the shell show after >>> 10 <= 9 ?
WHY THIS EXERCISE10 is neither less than nor equal to 9, so the answer is False.
At your computer
1. In the IDLE shell, type fuel = 40 and press Enter.
2. Type fuel >= 50 and press Enter. You should see False.
3. Try fuel != 0, then fuel == 40.
4. Try "go" == "Go". Then try "go" == "go".
5. Make up three comparisons. Predict True or False before you press Enter.

Practice: storing a comparison

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'>
READ CHECKS.PY
  • Read the question.
  • Tap your answer.
Why is fuel_ok False?
What type does the last line show?
Change wind to 25. What does Wind OK show?
temp = 18. What does temp > 18 give?
temp = 18. What does temp >= 18 give?
code = "A7". What does code == "a7" give?
StatementTrue 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.?
WHY THIS EXERCISETrue and False need capitals, and strings must match exactly, spaces included.

Every decision a program makes starts with a True or False question. Tomorrow you will use those answers with if and else.