Simple if-else question

Gary Herron gherron at islandtraining.com
Tue Sep 29 18:08:06 CEST 2009


Sandy wrote:
> Hi all,
> A simple and silly if-else question.
> I saw some code that has the following structure. My question is why
> else is used there though removing else
> has the same result. More important, is it not syntactically wrong :-(
>
> for i in xrange(8):
>     if i < 4:
>         print i
> else:
>     print i
>
> Cheers,
> dksr
>   

See 
http://docs.python.org/reference/compound_stmts.html#the-for-statement 
for an explanation of the "for" statement, and the meaning of it's 
"else" suite.

In this particular instance, the "else" adds nothing, but there are 
instances where it does provide a useful functionality.

Gary Herron




More information about the Python-list mailing list