[Tutor] (no subject)
joel.goldstick at gmail.com
Wed Feb 8 23:21:35 CET 2012
On Wed, Feb 8, 2012 at 2:04 PM, Patrick Dempster
<paddy at paddy-dempster.org.uk> wrote:
> On 07/02/2012 19:07, Hugo Arts wrote:
>> On Tue, Feb 7, 2012 at 7:50 PM, Debashish Saha <silideba at gmail.com> wrote:
>>> for i in range(1, 8):
>>> if i==3:
>>> print('The for loop is over')
>>> Question:but after breaking the for loop why the else command could not work?
>> because the else statement was designed to be that way:
>> quoting the relevant part:
>> "When the items are exhausted (which is immediately when the sequence
>> is empty), the suite in the else clause, if present, is executed, and
>> the loop terminates.
>> A break statement executed in the first suite terminates the loop
>> without executing the else clause’s suite."
>> in short, the else clause only executes if you do *not* break out of the loop.
> I might be missing something but I can't see a reason for the "else:"
> clause attached to the "for" statement, could anyone provide an example
> where or why someone might use the "else:" clause with the for loop?
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
Here is and interesting article:
The else clause runs if the loop breaks for some reason. So you would
use it only to do some processing if the loop completes completely.
More information about the Tutor