Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why doesn't Python use semicolons as Terminators?

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains why Python doesn't use a semicolon as a Terminator. Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why Python doesn't use a semicolon as a Terminator.

In general, the semicolon ";" is used in programming languages to achieve two purposes:

As a statement separator: use semicolons to separate statements (statement) so that you can write multiple statements (multiple sentences per line) in one line of code.

As a statement Terminator: use a semicolon to terminate the statement so that multiple lines of code can be recognized as a single statement (multiple lines of a sentence)

Just look at the "delimiter" and "Terminator". They are both necessary, but should they both be represented by semicolons? There is no agreed standard on this issue.

In Python, semicolons are used as statement delimiters, but instead of semicolons as Terminators, line breaks are used as Terminators.

If you add a semicolon at the end of a complete statement and then wrap the line, IDE will generally prompt "Trailing semicolon in the statement", indicating that the "trailing semicolon" is superfluous.

As I understand it, the trailing semicolon is actually treated as a delimiter, except that it is followed by an "empty statement" followed by a line break (that is, a Terminator). It is unnecessary to separate empty statements, so trailing semicolons become superfluous.

Python does not use a semicolon as a Terminator for the following reasons:

It regards indentation and line wrapping as effective parts of grammar, which can express complete semantics and will not lead to compile-time ambiguity. This is the main reason, which is fundamentally different from the "semicolon party".

Do not use semicolons and curly braces, but use indents and colons, which is a continuous line of thinking, resulting in higher readability, simplicity, and standardization in general. This reflects the relationship between local grammar and overall rules, 1 + 1 > 2.

You can write fewer characters and avoid the trouble of typing the "shift" key on some keyboards.

The semicolon is mainly for machines, but Python pays more attention to humanization. In the early days, there were restrictions on hardware, and the addition of semicolons could improve the speed of parsing / compilation, but now the obstacles have been removed, and some semicolon party languages are just continuing the old tradition of BWeiC language.

For statements that need to break lines, Python uses a backslash (\) to connect, which can be understood as escaping line breaks, which can solve the problem of multiple lines.

At this point, I believe you have a deeper understanding of why Python does not use a semicolon as a Terminator. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report