4 usages of an ELSE clause in Python
The existence of some of these may be a surprise for you.
Most of us know IF-ELSE syntax. But the IF statements are not the only place for using an ELSE clause. Haven’t you used an ELSE clause when writing loops? Or maybe when handling exceptions? If no, then you most probably will find out something new about the Python programming language in a couple of next paragraphs.
IF-ELSE
That is the most well-known usage of an ELSE clause. I won’t write much about it. If you are a Python programmer, you are using this every day.
Everything is clear and straight-forward. Let’s go to a little bit more interesting use case now.
FOR-ELSE
We are using a FOR keyword to create loops. But not everybody knows that there is an optional ELSE clause to a FOR loop. Let’s consider the following example.
As you can see, we are using an ELSE clause at the same level as the FOR keyword (not together with an IF statement). Code is executed successfully, but the code in the ELSE clause is not triggered.
Now let’s compare this with a slightly different example below.
As you can see, now the code in the ELSE clause is triggered, as the text “ELSE” is printed out. The only difference from the previous example is that the condition inside the FOR loop was never true, hence the BREAK statement didn’t execute and we looped through all the elements.
That is exactly what an ELSE clause is used for in FOR loops. This clause executes if a loop has iterated through all the elements. The intuition behind this logic is as follows — typically we break out of the loop when we have found an item of our interest, else (if we didn’t found it) we iterate through the loop fully.
WHILE-ELSE
An idea is very similar when using an ELSE clause in a WHILE loop. If we break out of the loop, the ELSE clause won’t execute as in the example below.
Let’s change the condition inside the loop in a way so that the BREAK statement is never triggered.
As expected, now we see the text “ELSE” printed out in the console.
So if the WHILE loop finishes because its condition evaluates to “False”, we get the ELSE clause executed. But if we break the loop or execute a RETURN statement from inside a function — the ELSE part is not executed.
TRY-EXCEPT-ELSE
Now, this use-case is a little bit different from the previous ones. We are typically using TRY keywords to handle exceptions. But we can use an ELSE clause in a TRY statement as well. Let’s start again with a simple example.
We can see the ELSE clause used in the TRY-EXCEPT statement, but as we can observe from the output — this clause didn’t get executed.
Let’s try now to modify our code in a way so that the ELSE clause executes.
Bingo! Now we see our ELSE clause in action. And we also got an intuition about how things work here. An ELSE clause in a TRY-EXCEPT statement is executed only if there was no exception thrown.
If we re-phrase it a bit so that an “Else” word semantically makes sense — if there is an exception, the EXCEPT clause is executed, else (if there were no exception) the ELSE part works.
Of course, we can use the FINALLY clause as well — this is executed in all the cases. So the full syntax example is as follows.
As expected, the text “FINALLY” is printed out in the console output.
One more IF-ELSE statement as a bonus
IF you learned at least one new Python code construction in this article: follow me not to miss other similar articles.
ELSE: it’s up to you to follow me or not.
Thanks for reading!