Why is a double semicolon a SyntaxError in Python? Why is a double semicolon a SyntaxError in Python? python python

Why is a double semicolon a SyntaxError in Python?


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; # Finex = 42;pass; # Finex = 42;; # Syntax errorif x == 42:; print("Yes") # Syntax error - "if x == 42:" isn't a complete statement


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

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