Why is a double semicolon a SyntaxError in Python?

Viewed 4041

I know that semicolons are unnecessary in Python, but they can be used to cram multiple statements onto a single line, e.g.

>>> x = 42; y = 54

I always thought that a semicolon was equivalent to a line break. So I was a bit surprised to learn (h/t Ned Batchelder on Twitter) that a double semicolon is a SyntaxError:

>>> x = 42
>>> x = 42;
>>> x = 42;;
  File "<stdin>", line 1
    x = 42;;
           ^
SyntaxError: invalid syntax

I assumed the last program was equivalent to x = 42\n\n. I’d have thought the statement between the semicolons was treated as an empty line, a no-op. Apparently not.

Why is this an error?

2 Answers
Related