This is simply not allowed by grammar . Relevant Manufacturing:
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
As you can see, after for you can either put a simple statement or a “set”, that is, a pending block. The if is a compound statement, not a simple one.
Two lines is the minimum to express this program:
for x in xrange(10): if x % 2 == 0: print x, 'is even'
(Of course, you can write equivalent programs that occupy only one line, for example
for x in xrange(0, 10, 2): print x, "is even"
or any other single line liner sent in response to this question.)
source share