Loop ... Else Clause, Partial Functions & Type Hints
Loop ... Else Clause
In Python, the else
clause in a loop is executed when the loop terminates normally (when a break statement is not executed). This is in contrast to the try
and except
statements, where the else
block is executed when no exception is raised.
For... else
In Python, the for statement is used to iterate over a sequence (such as a list, tuple, or string) or other iterable object. The else
clause in a for loop is optional and is executed only if the loop completed normally (that is, if the loop was not interrupted by a break statement).
The
else
clause would execute if our iteration or loop executes normally without breakingThe
else
clause will also execute if the loop is empty
While... else
In Python, the while loop executes a block of code repeatedly as long as a certain condition is true. The else
clause in a while loop in Python is optional and is executed only when the condition in the while loop becomes False. It is not executed if the loop is terminated by a break statement.
The else
clause in a loop can be a useful way to add some additional logic that should be executed only if the loop completes normally. However, it's important to note that the else
clause is not the same as the else
clause in a try
and except
block. The else
clause in a try
and except
block is executed when no exception is raised, whereas the else
clause in a loop is executed when the loop terminates normally.
Last updated