What is a Selection Statement?
Every program you have written so far runs every line it contains. A selection statement is the first one that does not: based on a condition, some statements are carried out and others are skipped completely. It is also called a conditional statement, and it is how a program stops being a fixed list of instructions and starts making a decision.
1A program that decides
Think about a shop bill. If the total is over ₹500, delivery is free. If it is not, delivery is charged. The same shop, the same rule, two different bills — and the shopkeeper does something different in each case.
You could not write that with what you know so far. A sequential program does the same thing every time it runs. To write the rule you need a way to say: run these lines only when this is true.
2It all hangs on the condition
The condition is the question being asked. It is nothing new: it is an expression whose value is True or False, exactly like the ones you built with the relational and logical operators.
# a condition is just an expression whose value is True or False
age = 19
print(age >= 16)
print(age == 16)
print(age < 16 or age > 60)True False False
Those three lines print their answers because we asked them to. Inside a selection statement nothing is printed — Python works the condition out for itself, and uses the True or False to decide what to run next.
age >= 16. Is the number even? → num % 2 == 0. Is the day Sunday? → day == 'Sunday'.3The shape of a selection statement
Every selection statement is built from the same parts. Tap them below — including the empty space at the start of the third line, which is a part in its own right.
Tap any part of the program — including the empty space — to see what it is called and what it does.
Nothing selected. Tap a coloured part above — the four spaces at the start of the third line count as a part too.
Two of those parts are new punctuation and both are compulsory. The colon ends the header. The indentation marks the block that belongs to it.
4Indentation is not decoration
In many languages, curly brackets say where a block starts and ends, and the spacing is only for looks. Python has no brackets here. The spacing is the language: a line indented under the header is inside the block, and a line back at the margin is outside it.
Which means one stray press of the space bar changes what your program does. Both programs below have a bill of 200, and only one of them prints anything at all:
bill = 200.0
if bill > 500:
print('You get free delivery!')
print('Amount to pay:', bill)Amount to pay: 200.0
# the last line has been indented by mistake
bill = 200.0
if bill > 500:
print('You get free delivery!')
print('Amount to pay:', bill)The second program prints nothing. Its last line is now indented, so it is part of the block — and the block was skipped, because 200 is not more than 500. Same statements, same order, one extra level of indentation, and the output disappears. No error is reported, because nothing is wrong with the program; it is simply not the program you meant to write.
A block is not limited to one statement. Every line indented under the header belongs to it, however many there are:
# a block can hold as many statements as you like
marks = 91
if marks >= 90:
print('Excellent!')
print('You are in the top grade.')
print('Marks =', marks)Excellent! You are in the top grade. Marks = 91
5Selection comes in three shapes
Now the part that decides which one you write. All three start with if and a condition, so they look alike on the page. What separates them is how many different things your program can end up doing.
They all start with if, so the syntax will not tell you which to write. Count the outcomes instead.
if
1 outcome“Do this — or do nothing extra.”
There is one block, and it either runs or it does not. The bill can be any amount at all — that is the many possibilities — but there is only one extra thing that can happen, and when the condition is False the program simply carries on with the line below.
Many possibilities, but only one thing the program might do about them.
| Shape | Outcomes | Use it when |
|---|---|---|
if | 1 | there is one thing that might happen — otherwise the program just carries on |
if…else | exactly 2 | there are two things it might do, and one of them is the default |
if…elif…else | many | there are more than two, and each one has its own test |
6Recap
True or False. The header ends in a colon, and the indented lines under it are the block that may run. There are three shapes — if, if…else and if…elif…else — and you pick between them by counting the outcomes.What decides whether the block under an if header runs?
A line under an if header is indented by 4 spaces. What does that indentation mean?
A program must print 'Pass' or 'Fail' and nothing else. Which shape fits?