The life of the “hated” semicolon: Stop using useless semicolons in Python

I see a lot of semicolons in C/C ++ languages. In code, a semicolon indicates statement termination. Python does not enforce the use of semicolons to separate statements, but there are plenty of instances of Python garbled characters because of semicolons.

Recently, I was taking a data science course in Python. The instructor explained conditional statements and wrote the following code:

temp =10; if temp <12: print( It is cold ); cold = True;Copy the code

My reaction at the time was: This is terrible! Semicolons are used only in atypical cases in Python. I have prepared a short guide to why semicolons should not be used in Python, listing a few special cases.

Statement terminator

In many popular programming languages, a semicolon is added to the end of each statement. For example, in C ++ :

int c =10; int a = 5; printf( In C++, semicolon at the end is must );Copy the code

This is not the case with Python. Python is a compact programming language, and you don’t need to add unnecessary characters and syntax. In Python, a statement ends at the end of a line (except square brackets, quotes, or parentheses). Such as:

c = 10
a = 5print( No semicolons in Python )
Copy the code

Statement separators The semicolon in Python indicates separation, not termination. It allows you to write multiple statements on the same line.

print( Statement1 ); print( Statement 2 ); print( Statement 3 )
Copy the code

This syntax allows a semicolon at the end of a single statement:

print( WhyGod? WHY? ) ;Copy the code

Print (…) “And terminates in the next sentence. So it’s actually two statements, the second of which is null. Even though the language allows semicolons to separate statements, most Python programmers do not use them in their code.

Python should be legible. Syntactic characters such as semicolons cause unnecessary confusion. If you send this code to an experienced Python programmer, he probably won’t talk to you again.

temp =10; cold =False;
              if temp<15: print( It is cold ); cold =True; print( Another statement )
          print(cold); print( Done );
Copy the code

Forcing multiple statements on a single line can make simple code hard to read.

When to use a semicolon?

So the question is, why are semicolons allowed in Python if they are ugly and cumbersome?

I think this is to make the transition between the two programming languages a little easier. Programmers with Java, C ++, and PHP backgrounds routinely place (useless) terminators at the end of every line.

However, semicolons also come in handy in the following situations:

Run the script from the Shell

The most common case is to use python-c

python-c import math; t = math.pi; Print (int (t)) 'Copy the code

Another use case is the exec statement:

Exec (fori in [100,200,300]: print(I); print(i*2) )Copy the code

Evaluate the side effects of the expression

When working in an interactive environment like Jupyter Notebook, the value returned by the last expression is printed as a string. Evaluating the side effects of a function can be really annoying, and looking at the problem in Matplotlib can be cumbersome.

I will create a simple histogram in JupyterNotebook. As shown in the figure below, running this script adds an extra line to the output: Text (0.5, 1.0, Histogram).

This is actually calling.set_title (…) The return value of the function. To suppress the output, add a semicolon to the end of the last statement:

axis.set_title( Histogram ,size=10);
Copy the code

Brevity is greatness, and we should avoid adding any extra characters when we write code. Cut out the extra semicolons and use them only when necessary.

※ Some articles from the network, if any infringement, please contact to delete; More articles and materials | click behind the text to the left left left 100 gpython self-study data package Ali cloud K8s practical manual guide] [ali cloud CDN row pit CDN ECS Hadoop large data of actual combat operations guide the conversation practice manual manual Knative cloud native application development guide OSS Operation and maintenance actual combat manual cloud native architecture white paper Zabbix enterprise distributed monitoring system source document 10G large factory interview questions