Why can’t use semi-colon before for loop in Python?

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.

Leave a Comment