In general, programming languages use the semicolon “;” To achieve two purposes:

  1. As statement delimiters: Use semicolons to separate statements so that multiple statements can be written in one line of code.
  2. As a statement terminator: Terminates a statement with a semicolon so that multiple lines of code are identified as one statement (a single line)

Just look at “delimiters” and “terminators”, they are both necessary, but should they all be represented by semicolons? There is no agreed standard on this issue.

Python uses semicolons as statement separators, but instead of semicolons as terminators, newlines are used as terminators.

If a semicolon is appended to the end of a complete statement and then a line break, the IDE usually prompts “Trailing semicolon in the statement,” indicating that the “Trailing semicolon” is unnecessary.

As I understand it, the trailing semicolon is actually treated as a separator, except that it is followed by a “null statement” followed by a newline (terminator). Separating empty statements is unnecessary, so trailing semicolons are redundant.

Python does not use semicolons as terminators for the following reasons:

  1. It makes indentation and line-breaking a valid part of the written law, which can express complete semantics without causing compile-time ambiguity. This is the most important reason and is the fundamental difference with the “semicolon Party”
  2. Instead of semicolons and curly braces, the use of indentation and colons is a consistent approach, resulting in overall greater readability, conciseness, and standardization. This shows the relationship between local grammar and overall rules, 1 + 1 > 2
  3. You can write fewer characters and avoid the hassle of hitting the “shift” key on some keyboards
  4. Semicolons are mostly for machines, but Python is more about humanity. In the early days of hardware, semicolons were used to speed up parsing/compiling, but now that barrier has been removed, some semicolons are just a continuation of the B/C legacy
  5. Python uses a backslash (\) to concatenate statements that require line breaks, which can be interpreted as escaping a line break and solving the problem of multiple lines

Related reading

1. A Brief History of the Semicolon in Programming

2, The End of The Semicolon Era

Write in the last

This article is part of the “Why Python” series, which focuses on the syntax, design, and development of Python. The series tries to show the charm of Python by asking “why” questions. More details: Video address