CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 05 / BEGINNER PYTHON

Python if statements, comparisons and Booleans

A Boolean is either True or False. Conditions let a program choose which instructions to run using those values.

The idea

Tony’s door may open only when two facts hold: the reader has a key and the code is correct. Write down the facts separately before combining them. == compares values, != means not equal, and < and > compare order. and requires both conditions; or accepts either; not reverses a truth value. You can trace these small questions without guessing what the whole program does.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
has_key = True
code = "407"
if has_key and code == "407":
    print("Enter the workshop.")
else:
    print("Check the evidence.")
Run and change this example ↗
Reveal the expected output
Enter the workshop.

Read it one step at a time

  1. The two variables describe the situation before the decision.
  2. The condition asks two questions. Both are true, so Python runs the first block.
  3. The else block is the alternative. It is aligned with if, not with the indented print.

Make one change

Change only has_key to False. Then restore it and change only the code. What happens each time?

Check your thinking

Either change makes the combined condition false, so both experiments print Check the evidence.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.