Why is a double semicolon a SyntaxError in Python?

Solution 1:

From the Python grammar, we can see that ; is not defined as \n. The parser expects another statement after a ;, except if there's a newline after it:

                     Semicolon w/ statement    Maybe a semicolon  Newline
                          \/     \/               \/                \/
simple_stmt: small_stmt (';' small_stmt)*        [';']            NEWLINE

That's why x=42;; doesn't work; because there isn't a statement between the two semicolons, as "nothing" isn't a statement. If there was any complete statement between them, like a pass or even just a 0, the code would work.

x = 42;0; # Fine
x = 42;pass; # Fine
x = 42;; # Syntax error

if x == 42:; print("Yes") # Syntax error - "if x == 42:" isn't a complete statement

Solution 2:

An empty statement still needs pass, even if you have a semicolon.

>>> x = 42;pass;
>>> x
42