I can join lines in Python using semi-colon, e.g.
a=5; b=10
But why can't I do the same with for
x=['a','b']; for i,j in enumerate(x): print(i,":", j)
Because the Python grammar disallows it. See the documentation:
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
Semicolons can only be used to separate simple statements (not compound statements like for). And, really, there's almost no reason to ever use them even for that. Just use separate lines. Python isn't designed to make it convenient to jam lots of code onto one line.
for
1.4m articles
1.4m replys
5 comments
56.9k users