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,elifandelse - Indent a block correctly, and read an
IndentationError - Combine conditions with
and,orandnot, 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.
| Write | Means |
|---|---|
== | 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.
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.
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.
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.
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.
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.
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.
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.
- The Python Tutorial: More Control Flow Tools, if Statements
- The Python Language Reference: The if statement
- Built-in Types: Comparisons
- Built-in Types: Boolean Operations (and, or, not)
- Built-in Types: Truth Value Testing: which values count as false
- The Python Language Reference: Indentation
- Built-in Exceptions: IndentationError
- The Rust Reference: if expressions: the condition of a Rust if must be a bool