3.1.1.7 Making decisions in Python
Conditonal execution: the if statement
If a certain sleepless Python developer falls asleep when he or she counts 120 sheep, and the sleep-inducing procedure may be implemented as a special function named
sleep_and_dream()
, the whole code takes the following shape:if sheep_counter >= 120: # evaluate a test expression
sleep_and_dream() # execute if test expression is True
You can read it as: if
sheep_counter
is greater than or equal to 120
, then fall asleep and dream (i.e., execute the sleep_and_dream
function.)
We've said that conditionally executed statements have to be indented. This creates a very legible structure, clearly demonstrating all possible execution paths in the code.
Take a look at the following code:
if sheep_counter >= 120:
make_a_bed()
take_a_shower()
sleep_and_dream()
feed_the_sheepdogs()
As you can see, making a bed, taking a shower and falling asleep and dreaming are all executed conditionally - when
sheep_counter
reaches the desired limit.
Feeding the sheepdogs, however, is always done (i.e., the
feed_the_sheepdogs()
function is not indented and does not belong to the if
block, which means it is always executed.)
Now we're going to discuss another variant of the conditional statement, which also allows you to perform an additional action when the condition is not met.
Conditional execution: the if-else statement
We started out with a simple phrase which read: If the weather is good, we will go for a walk.
Note - there is not a word about what will happen if the weather is bad. We only know that we won't go outdoors, but what we could do instead is not known. We may want to plan something in case of bad weather, too.
We can say, for example: If the weather is good, we will go for a walk, otherwise we will go to a theater.
Now we know what we'll do if the conditions are met, and we know what we'll do if not everything goes our way. In other words, we have a "Plan B".
Python allows us to express such alternative plans. This is done with a second, slightly more complex form of the conditional statement, the if-else statement:
if true_or_false_condition:
perform_if_condition_true
else:
perform_if_condition_false
Thus, there is a new word:
else
- this is a keyword.
The part of the code which begins with
else
says what to do if the condition specified for the if
is not met (note the colon after the word).
The if-else execution goes as follows:
- if the condition evaluates to True (its value is not equal to zero), the
perform_if_condition_true
statement is executed, and the conditional statement comes to an end; - if the condition evaluates to False (it is equal to zero), the
perform_if_condition_false
statement is executed, and the conditional statement comes to an end.
Comments
Post a Comment