3.3 Branching (if, elif and else)
6 9
Python will proceed immediately by also testing the second condition. However,
this test is superfluous, since we know beforehand that it will evaluate to True! So,
in this particular case, using two separate if tests is not suitable (generally, however,
separate if tests may be just what you need. It all depends on the problem at hand).
An if-else Construction For our case, it is much better to use an else part, like
this
T = float(input(’What is the water temperature? ’))
if T > 24:
# testing condition 1
print(’Great, jump in!’)
else:
print(’Do not swim. Too cold!’)
# First line after if-else construction
When the first condition evaluates to False in this code, execution proceeds directly
with the print command in the else part, with no extra testing!
To students with little programming experience, this may seem like a very small
thing to shout about. However, in addition to avoiding an unnecessary test with
the if-else alternative, it also corresponds better to the actual logic: If the first
condition is false, then the other condition has to be true, and vice versa. No further
checking is needed.
An if-elif-else Construction Considering our “advisor program”, we have to admit
it is a bit crude, having only two categories. If the temperature is larger than 24
degrees, we are advised to swim, otherwise not. Some refinement seems to be the
thing.
Let us say we allow some intermediate case, in which our program is less
categoric for temperatures between 20 and 24 degrees, for example. There is a nice
elif (short for else if) construction which then applies. Introducing that in our
program (and saving it as swim_advisor.py), it reads
T = float(input(’What is the water temperature? ’))
if T > 24:
# testing condition 1
print(’Great, jump in!’)
elif 20 <= T <= 24:
# testing condition 2
print(’Not bad. Put your toe in first!’)
else:
print(’Do not swim. Too cold!’)
# First line after if-elif-else construction
You probably realize what will happen now. For temperatures above 24 and below
20, our “advisor” will respond just like in the previous version (i.e., the if-else
version). However, for intermediate temperatures, the first condition will evaluate to
False, which implies that the Python interpreter will continue with the elif line.
Here, condition 2 will evaluate to True, which means that “Not bad. Put your toe
in first!” will be printed. The else part is then skipped. As you might expect, more
refinement would be straight forward to include by use of more elif parts.
Programming as a Step-Wise Process The reader should note that, besides
demonstrating branching, the development of the previous program gave a (very)
simple example of how code may be written by a step-wise approach. Starting
6 9
Python will proceed immediately by also testing the second condition. However,
this test is superfluous, since we know beforehand that it will evaluate to True! So,
in this particular case, using two separate if tests is not suitable (generally, however,
separate if tests may be just what you need. It all depends on the problem at hand).
An if-else Construction For our case, it is much better to use an else part, like
this
T = float(input(’What is the water temperature? ’))
if T > 24:
# testing condition 1
print(’Great, jump in!’)
else:
print(’Do not swim. Too cold!’)
# First line after if-else construction
When the first condition evaluates to False in this code, execution proceeds directly
with the print command in the else part, with no extra testing!
To students with little programming experience, this may seem like a very small
thing to shout about. However, in addition to avoiding an unnecessary test with
the if-else alternative, it also corresponds better to the actual logic: If the first
condition is false, then the other condition has to be true, and vice versa. No further
checking is needed.
An if-elif-else Construction Considering our “advisor program”, we have to admit
it is a bit crude, having only two categories. If the temperature is larger than 24
degrees, we are advised to swim, otherwise not. Some refinement seems to be the
thing.
Let us say we allow some intermediate case, in which our program is less
categoric for temperatures between 20 and 24 degrees, for example. There is a nice
elif (short for else if) construction which then applies. Introducing that in our
program (and saving it as swim_advisor.py), it reads
T = float(input(’What is the water temperature? ’))
if T > 24:
# testing condition 1
print(’Great, jump in!’)
elif 20 <= T <= 24:
# testing condition 2
print(’Not bad. Put your toe in first!’)
else:
print(’Do not swim. Too cold!’)
# First line after if-elif-else construction
You probably realize what will happen now. For temperatures above 24 and below
20, our “advisor” will respond just like in the previous version (i.e., the if-else
version). However, for intermediate temperatures, the first condition will evaluate to
False, which implies that the Python interpreter will continue with the elif line.
Here, condition 2 will evaluate to True, which means that “Not bad. Put your toe
in first!” will be printed. The else part is then skipped. As you might expect, more
refinement would be straight forward to include by use of more elif parts.
Programming as a Step-Wise Process The reader should note that, besides
demonstrating branching, the development of the previous program gave a (very)
simple example of how code may be written by a step-wise approach. Starting
