Reputation: 16053
>>> query='';for var in xrange(9):\n\tquery+=str(var)
File "<stdin>", line 1
query='';for var in xrange(9):\n\tquery+=str(var)
^
SyntaxError: invalid syntax
>>> query='';for var in xrange(9): query+=str(var)
File "<stdin>", line 1
query='';for var in xrange(9): query+=str(var)
^
SyntaxError: invalid syntax
Why wont the above code work? But the following works
>>> query=""
>>> for var in xrange(9): query+=str(var)
...
>>> query
'012345678'
>>>
Upvotes: 2
Views: 170
Reputation: 82939
The ;
is only allowed to combine "small statements". Those are expressions, print, and the like. A for
loop, on the other hand, is a compound statement. See the Full Grammar Specification:
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | exec_stmt | assert_stmt)
...
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
In your example, if this is more than just to illustrate your question, you could rewrite it as
query = ''.join(str(var) for var in xrange(9))
Or if you really really need to exec
that multiline statement, you can add a \n
between the assignment and the for
loop (as you already did in another place):
>>> exec("query=''\nfor var in xrange(9):\n\tquery+=str(var)\nprint query")
012345678
But note that this does only work in exec
, not in the interactive shell directly:
>>> query=''\nfor var in xrange(9):\n\tquery+=str(var)\nprint query
SyntaxError: unexpected character after line continuation character
Upvotes: 4
Reputation: 76254
Perhaps you wanted a generator expression:
>>> query = "".join(str(var) for var in xrange(9))
>>> query
'012345678'
Upvotes: 3
Reputation: 2981
The vast majority of Python programmers only use line breaks to separate statements, and I suspect most don't even know the semicolon is a possibility.
And (as Abhijit said) that feature is limited: it only works for simple statements like variable assignments and function calls, not for control statements like "if", "for", "while", etc.
Upvotes: 0
Reputation: 63777
A for statement is a compound statement which by definition cannot follow a semicolon
Quoting the python documentation
Compound statements consist of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn’t be clear to which if clause a following else clause would belong:
and it so happens that a for statement is a compound statement in Python
compound_stmt ::= if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| funcdef
| classdef
| decorated
and so it cannot follow a semicolon
Upvotes: 2