[Tutor] SEE THE QUESTION AT THE BOTTOM

Steve Willoughby steve at alchemy.com
Sat Feb 4 06:53:03 CET 2012


On 03-Feb-12 21:38, Debashish Saha wrote:
> BUT I COULD NOT UNDERSTAND HOW THE COMMAND ELSE CAN WORK,THOUGH IT IS IN
> THE OUTSIDE OF THE FOR LOOP IN WHICH IF COMMAND LIES.

The part that's confusing you is that it is not outside the for loop. 
It is PART of the for loop syntax.  The loop construct used is:

for <variable> in <list>:
	<list body>
else:
	<else body>

This means you run <variable. through each element of <list>, executing 
<list body> once for each iteration, and a statement in that body may 
elect to break out of the loop prematurely.  If nothing breaks out of 
the loop (i.e., you exit the loop because the <list> was exhausted), 
then and only then execute <else body>.

It's a handy mechanism to handle the case where the for loop failed to 
find whatever it was searching for.  Most other languages I've used 
don't have this, and you end up doing messier manual logic steps to 
accomplish the same thing.

Tip:  Please don't type a message IN ALL CAPITAL LETTERS; it gives the 
impression you are shouting at your audience.

HTH,
HAND

-- 
Steve Willoughby / steve at alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C


More information about the Tutor mailing list