I was playing some code golf and came across behavior I don't understand.
Why is the following code valid:
import os; print(1)
and this code is valid:
import os
for i in range(10): print(1)
but not:
import os; for i in range(10): print(1)
It errors out with:
File "<stdin>", line 1
import os; for i in range(10): print(1)
^^^
SyntaxError: invalid syntax
It breaks my mental model of how the semicolon works in python, shouldn't this be valid? If not, could someone explain why? Is there something special about the for
and while
keywords that prohibit oneliners?
What's a work around for playing code golf using python?