Every program you have written so far runs the same way every time. Python starts at the top, runs each line in turn, and reaches the bottom. Nothing is ever skipped, whatever the user types.
Real instructions are not like that. They contain decisions:
IF it is raining THEN
take your umbrella.
IF you haven’t done your homework THEN
you get grounded.
In both of those, the instruction on the second line only happens when the sentence on the first line is true. Otherwise it is skipped. Code that makes the computer do different things in different situations is called selection, and every piece of selection starts with the part that does the deciding: the condition.
“It is raining” is a check with exactly two possible answers. Either it is
raining, or it is not. There is no third option and no maybe. A check like that
is called a Boolean condition, and its answer is one of Python’s two
Boolean values: True or False.
Boolean is written with a capital B because it is named after George Boole, the
mathematician who worked out the rules for true and false values. So are
Python’s True and False, which are always spelled with a capital letter.
In the umbrella example, the condition is “it is raining”. If it comes back true, the instruction below it is carried out. If it comes back false, that instruction is skipped and the computer carries on with whatever comes next.
Conditions are written with comparison operators. Each one takes the two
pieces of data on either side of it and answers one question about them with
True or False.
| Operator | The question it asks | Example | Result |
|---|---|---|---|
== |
equal to, the same as | 5 == 5 |
True |
!= |
not equal to | 5 != 5 |
False |
> |
greater than | 7 > 5 |
True |
>= |
greater than or equal to | 5 >= 5 |
True |
< |
less than | 7 < 5 |
False |
<= |
less than or equal to | 5 <= 5 |
True |
The first two, == and !=, work on numbers and on text. The other four are
for comparing numbers, because they ask which of the two is bigger, and that is
a question about size.
Either side of a comparison can be a variable rather than a value typed out in full, which is what makes conditions useful. The umbrella rule written with a variable looks like this:
IF
weather == "rain"THEN
take your umbrella.
The computer looks inside weather, compares whatever it finds with "rain",
and gets True or False back.
A single comparison only checks one side of a number: whether it is big
enough, or whether it is small enough. To check that a number lies inside a
range, for example between 1 and 10, you need two comparisons, one for the
bottom of the range and one for the top, joined together with and:
num >= 1 and num <= 10
The whole condition is only True when both of the comparisons around the
and are True:
Value of num |
num >= 1 |
num <= 10 |
Whole condition |
|---|---|---|---|
7 |
True |
True |
True |
15 |
True |
False |
False |
0 |
False |
True |
False |
One False is enough to make the whole condition False, so 15 is turned away
for being too big and 0 for being too small.
Use >= and <= when the numbers at both ends of the range belong in it
themselves, the way 1 and 10 do here.
This is the mistake that catches almost everybody at least once.
A single = is the assignment symbol. It stores the data on its right in
the variable on its left, and it changes what the variable holds:
weather = "rain"
A double == is a comparison. It changes nothing at all. It looks at the
two pieces of data and answers one question about them:
weather == "rain"
Read them out loud and the difference is clear. The first one says “let the
weather be rain”. The second one asks “is the weather rain?”. A condition always
needs the question, so a condition always needs ==.
Writing a single = where a condition belongs is not a mistake Python quietly
lets through. It stops the program with a SyntaxError before anything runs at
all, and modern versions of Python even guess what you meant:
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
When == compares two pieces of text, the match has to be exact. Every
character has to be the same, and a capital letter is a different character from
the small letter that looks like it.
So if the user types rain, the condition weather == "rain" is True. If
they type Rain, that same condition is False, because R is not r. So is
RAIN, and so is rain with a stray space on the end.
"rain" and "Rain" are two different pieces of text as far as == is
concerned. Whenever you compare something the user typed, decide which exact
spelling your program is looking for, and tell the user what to type. A later
unit looks at how to accept both spellings at once.
Run the code below. Every line outputs True or False, and nothing else,
because that is all a condition ever produces.
weather = "rain" print(weather == "rain") print(weather == "Rain") print(weather != "rain") temperature = 12 print(temperature > 20) print(temperature < 20) print(temperature >= 12) print(temperature >= 10 and temperature <= 20)
Work out what each of the seven answers should be before you run it. Then change
weather to "sun" and temperature to 25, and work them all out again.
Python also accepts the shorter 10 <= temperature <= 20, and the editor may suggest it
on the last line. Both work the same; this course uses and, the way the exam writes it.
Make sure that you check for the following things:
==) to compare two pieces of data, and a
single = only to store data in a variable.True and False are spelled with a capital letter.and is a complete comparison with the
variable written out again: num >= 1 and num <= 10, not
num >= 1 and <= 10.