Conditions and conditional execution
You already know how to ask Python questions, but you still don't know how to make reasonable use of the answers. You have to have a mechanism which will allow you to do something if a condition is met, and not do it if it isn't.
It's just like in real life: you do certain things or you don't when a specific condition is met or not, e.g., you go for a walk if the weather is good, or stay home if it's wet and cold
To make such decisions, Python offers a special instruction. Due to its nature and its application, it's called a conditional instruction (or conditional statement).
There are several variants of it. We'll start with the simplest, increasing the difficulty slowly.
The first form of conditional statement, which you can see below is written very informally but figuratively:
if true_or_not:
do_this_if_true
This conditional statement consists of the following, strictly necessary, elements in this and this order only:
How does the that statement work?
In real life, we often express desire:
if the weather is good, we'll go for a walk
then, we'll have lunch
As you can see, having lunch is not a conditional activity and doesn't depend on the weather.
Knowing what conditions influence our behavior, and assuming that we have the parameterless functions go_for_a_walk() and have_lunch(), we can write the following snippet:
if the_weather_is_good:
go_for_a_walk()
have_lunch()
Conditional 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.
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 ofrm 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: