In this topic students meet selection: the code that makes the computer perform different tasks in different situations, built on conditions that are either true or false.
Be able to read, comprehend, trace, adapt and create Python code using selection that:
This is the one chapter whose overview page carries no link to a slide deck. The other chapters’ overview pages each end with a Google Slides link; this one does not, so there is no deck link to pass on here.
The deck itself does exist and is short. Its content is in the Boolean conditions and Selection reading activities at the start of this chapter.
This section is focused on selection, the code that makes the computer perform different tasks in different situations. Selection is sometimes referred to as a decision, and this often helps when we are explaining the idea to students. Whenever we make an IF this THEN that type decision we are using selection logic. There are a lot of concepts to unpack in even a simple selection statement. It is worth breaking it down and building up slowly, and students often need a lot of time and practice before they truly master it.
For example, real life decisions could be:
IF it is raining THEN
take your umbrella.
Or:
IF you haven’t done your homework THEN
you get grounded.
In both of these examples, the instruction on the second line is only followed if the scenario on the first line is true. Otherwise it is skipped over. This is an essential part of selection, explained in more depth below.
Whereas in all of the code we have written before the computer runs each line one at a time, selection relies on a Boolean condition to decide whether or not to run the code.
Boolean conditions are true or false checks. If a condition is checked and comes back as true, the code on the line or lines below runs. If it comes back as false, then the computer skips those commands and moves on with the rest of the program.
Putting that into practice with the umbrella example above:
IF it is raining THEN
take your umbrella
“It is raining” is the condition. It is either raining (true) or not (false). If it is raining, then the instruction to take your umbrella is carried out. If not, then we skip down to the next instruction that is not part of the selection (probably something like “leave the house”).
Boolean conditions are written using comparison operators. These are:
== equal to, the same as. A single = is used to store data in a variable,
so Python uses == to compare two pieces of data.!= not equal to.The two above can be used with integers or with strings. The four below are for comparing numeric data (integers, or floats and decimals):
> greater than>= greater than or equal to< less than<= less than or equal toUsing the umbrella example again: we want to compare the weather to rain to see if they are the same, so we would write the condition like this:
IF
weather == "rain"THEN
take your umbrella
Case matters when comparing text with ==. The match has to be exact.
For the example above, if the user inputs rain then the condition will be true.
If they input Rain then it will be false. Future units look at how to get
around this issue.
The examples above only refer to selection for one situation. See the individual task notes for how to develop this for two or more situations.
These selection examples either output a statement (if the condition is true) or
they appear to do nothing (if the condition is false). In reality the computer is
just skipping the indented line or lines below the if. Because there is no more
code after this, the computer has no more instructions to execute and so
completes the program. Students should only get an output if their condition is
true.
The shape is an if, a condition, a colon, and then one or more indented lines
underneath:
if weather == "rain":
print("Take your umbrella")
These selection examples output one of two different statements. The indented
code below the if runs when the condition is true. The else is used as a
catch all: if the condition is false then the computer skips down to the else
and runs the indented code there instead. Please note, the else does not
have a condition. It does not need one, because it runs every time the condition
after the if is false.
if weather == "rain":
print("Take your umbrella")
else:
print("No special advice for you")
Make sure that students check for the following things:
if and else line.else.