Simple if-else question

Carl Banks pavlovevidence at
Tue Sep 29 19:15:49 CEST 2009

On Sep 29, 9:08 am, Gary Herron <gher... at> wrote:
> 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
> 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.

Hmm, I wonder if Python should emit a warning if an else is used on a
for block with no break inside.  I don't think the else can be invoked
in any other way.  As a bonus it could catch some cases where people
mistakenly use it thinking it will execute when there are no

Carl Banks

More information about the Python-list mailing list