2. Making Choices

Programs get interesting when they can make decisions. “If the creature is hungry, look for food.” This page shows how.


True and False

A decision is always about something being True or False. In Python these are special words (with a capital first letter):

is_alive = True
is_hungry = False

Comparing things

To make a decision, you compare values. Each comparison gives back True or False.

SymbolMeansExampleResult
==is equal to5 == 5True
!=is not equal to5 != 3True
>is greater than10 > 3True
<is less than10 < 3False
>=is greater than or equal to5 >= 5True
<=is less than or equal to4 <= 5True

⚠️ Watch out: one = puts a value in a box. Two == asks “are these the same?”. They are different!

creature_energy = 100      # ONE = puts 100 in the box
print(creature_energy == 100)   # TWO == asks a question → True

if — do something only when it’s true

An if runs the code inside it only if the test is True.

creature_energy = 20

if creature_energy < 30:
    print("This creature is getting weak!")

Output:

This creature is getting weak!

Two very important rules

  1. The line with if ends in a colon :
  2. The code that belongs to the if is indented (pushed in) by 4 spaces.

That indent is how Python knows which lines belong to the if. If you forget it, you’ll get an error.

creature_energy = 50

if creature_energy < 30:
    print("This line is indented, so it belongs to the if.")
    print("So does this one.")

print("This line is NOT indented, so it always runs.")

Output:

This line is NOT indented, so it always runs.

(The first two lines were skipped because 50 is not less than 30.)


if / else — do one thing OR another

else gives you a “otherwise” path. One of the two blocks will always run.

creature_energy = 50

if creature_energy > 0:
    print("The creature is alive.")
else:
    print("The creature has died.")

Output:

The creature is alive.

if / elif / else — choosing between many options

elif means “else, if…”. Use it to check several things in order. Python checks them from top to bottom and runs the first one that is True, then stops.

creature_energy = 65

if creature_energy > 80:
    print("The creature is full of energy!")
elif creature_energy > 40:
    print("The creature is doing okay.")
elif creature_energy > 0:
    print("The creature is tired and hungry.")
else:
    print("The creature has run out of energy.")

Output:

The creature is doing okay.

You can have as many elif lines as you want. The else at the end is optional — it catches “none of the above”.


Combining tests: and, or, not

Sometimes one test isn’t enough.

and — both must be true:

creature_energy = 90
is_adult = True

if creature_energy > 50 and is_adult:
    print("This creature has enough energy to have a baby.")

or — at least one must be true:

creature_energy = 10
is_injured = False

if creature_energy < 20 or is_injured:
    print("This creature is in danger.")

not — flips True to False and back:

is_hungry = False

if not is_hungry:
    print("The creature is happily full.")

✅ Quick recap

  • Comparisons like ==, >, < give back True or False.
  • if runs code only when the test is True.
  • Add else for an “otherwise” path.
  • Add elif to check more options in order.
  • Remember the colon : and the 4-space indent.
  • Join tests with and, or, and not.