Basics · lesson 6 of 10
Parts in this lesson
A== != < >comparisons give True or False
Bifrun only when true
Celif elseother paths
D: ····a colon, then an indented block
Eand or notcombine conditions

Making decisions

Last updated

A program that always does the same thing isn't much use. In this lesson you'll compare values, run lines only when something is true, choose between several paths, and see why the spaces at the start of a line matter so much in Python.

By the end of this lesson you can

  • Compare values with ==, !=, <, >, <= and >=
  • Choose what runs with if, elif and else
  • Indent a block correctly, and read an IndentationError
  • Combine conditions with and, or and not, and know which values count as false

True or false

A comparison asks a question, and the answer is one of two values: True or False. These two have their own type, bool, named after George Boole, who worked out the maths of true and false.

WriteMeans
==is equal to
!=is not equal to
< and >is less than, is more than
<= and >=is less than or equal to, is more than or equal to

Equality is ==, two equals signs. One = gives a name a value (lesson 1.3), so Python needs a different symbol for asking. Line 5 shows that text comparisons care about capitals.

Step 1 code main.py
Edit the code, then run it again

Run lines only when: if

if, then a condition, then a colon, starts a block: the indented lines under it. The block runs only when the condition is True. Line 5 isn't indented, so it isn't part of the block, and it runs either way.

Press Watch it run and step through. Then change days_late to 0 and watch again: Python checks line 2, finds it False, and jumps straight to line 5.

Step 2 code main.py
Edit the code, then run it again

Other paths: elif and else

else: gives a block that runs when the if was False. elif, short for else if, adds more conditions in between. Python checks them from the top and runs only the first block whose condition is true, then skips the rest.

Step 3 code main.py
Edit the code, then run it again

Try days_late as 20, then 3, then 0. With 20, both days_late > 14 and days_late > 0 are true, but only the first block runs. That's why the bigger check comes first.

Indentation is part of the code

In Python, the spaces at the start of a line are part of the code. The colon at the end of the if line says "a block starts here", and the block is every line under it that's indented, usually by four spaces. Many languages use { and } for this, but Python uses the indentation itself, so it always matches what the code means.

This program forgot to indent line 3. Press Run and read the error.

Step 4 code main.py
Edit the code, then run it again

IndentationError: expected an indented block after 'if' statement on line 2. Put four spaces at the start of line 3, then run it again. (In this editor, the Tab key types four spaces.)

Now indent line 4 as well and run it with days_late = 0. Thank you! disappears, because it's now inside the block. One indent changes what the program does.

Combining conditions

and is true only when both sides are true. or is true when at least one side is. not flips True to False and back.

A member can borrow a book if they're a member and have fewer than 5 out. Run it, then change books_out to 5, and then is_member to False.

Step 5 code main.py
Edit the code, then run it again

Python's words read almost like English, but be careful with or: if day == "Sat" or "Sun": doesn't do what it seems to. Each side of or is a separate condition, so write if day == "Sat" or day == "Sun":.

Values that count as false

An if doesn't need a True or False. Python treats some values as false: 0, 0.0, empty text "" and, as you'll see later, empty lists. Everything else counts as true. Programmers call these falsy and truthy values.

Step 6 code main.py
Edit the code, then run it again

So if title: means "if title isn't empty". It's short and common in Python code, but write if copies == 0: when that says what you mean more clearly. (Rust, from the other course, doesn't allow this: an if there must be a real bool.)

Check what you learned

Four questions. Pick or type an answer, then check it.

What does this program print?
days = 20
if days > 14:
    print("A")
elif days > 7:
    print("B")
else:
    print("C")
Which line asks whether copies is equal to 0?
is_member = True and books_out = 6. Is is_member and books_out < 5 true?
Which of these counts as false in an if?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python