Detailed usage of comments (multi-line comments and single-line comments) in Python

Table of contents

Python single-line comments

Python multi-line comments

Precautions

Comments can help debug programs


Comments (Comments) are used to prompt or explain the role and function of some code to the user, and it can appear anywhere in the code. The Python interpreter ignores the comment when executing the code and does nothing as if it didn't exist.
In the process of debugging (Debug) programs, comments can also be used to temporarily remove useless code.
The biggest function of comments is to improve the readability of the program. A program without comments is simply a book, which makes people vomit blood!
Don't think that the code specification you write can be uncommented. Giving someone else a piece of code without comments is disrespectful to others and a very selfish behavior; you can like to abuse yourself, but please don't abuse others.
Many programmers would rather develop an application by themselves than modify other people's code. The lack of reasonable comments is an important reason. Although good code can be documented by itself, we will never know who is reading this code in the future, and whether he has the same idea as you; or after a period of time, you yourself will not know the purpose of writing this code at that time up.
In general, reasonable code comments should account for about 1/3 of the source code.
Python supports two types of comments, single-line comments and multi-line comments.

Python single-line comments

Python uses the pound sign #as a symbol for single-line comments, and the syntax format is

# Comment content

#Everything from the pound sign until the end of the line is a comment. When encountered by the Python interpreter #, the entire line following it is ignored.
When explaining the function of multiple lines of code, the comment is generally placed on the previous line of the code, for example: 

#使用print输出字符串
print("Hello World!")
#使用 print输出数字
print(100)
print( 3 + 100 * 2)
print( (3 + 100) * 2 )

When explaining the function of a single line of code, comments are generally placed on the right side of the code, for example:

print( 36.7 * 14.5 )  #输出乘积
print( 100 % 7 )  #输出余数

Python multi-line comments

A multi-line comment refers to the content of multiple lines (including one line) in a one-time comment program.

Python uses three consecutive single quotes ''' or three consecutive double quotes """ to comment multi-line content. The specific format is as follows:

'''
使用 3 个单引号分别作为注释的开头和结尾
可以一次性注释多行内容
这里面的内容全部是注释内容
'''

or

"""
使用 3 个双引号分别作为注释的开头和结尾
可以一次性注释多行内容
这里面的内容全部是注释内容
"""

Multi-line comments are usually used to add copyright or functional description information to Python files, modules, classes, or functions.

Precautions

1) Python multi-line comments do not support nesting, so the following writing is wrong:

'''
外层注释
    '''
    内层注释
    '''
'''

2) Regardless of whether it is a multi-line comment or a single-line comment, when comment characters appear as part of a string, they can no longer be regarded as comment marks, but should be regarded as part of normal code, for example:

print('''Hello,World!''')
print("#是单行注释的开始")

operation result:

Hello, World!
# is the beginning of a single-line comment

For the first two lines of code, Python does not treat the three quotation marks here as multi-line comments, but as the beginning and end of the string.

For the third line of code, Python does not treat the pound sign as a single-line comment, but as part of the string.

Comments can help debug programs

Adding instructions to the code is the basic function of comments. In addition, it has another practical function, which is used to debug programs.

For example, if you think there may be a problem with a certain piece of code, you can comment this code first, let the Python interpreter ignore this code, and then run it. If the program can be executed normally, it can be explained that the error is caused by this code; on the contrary, if the same error still occurs, it can be explained that the error is not caused by this code.

Using comments in the process of debugging programs can narrow down the scope of errors and improve the efficiency of debugging programs. 

Guess you like

Origin blog.csdn.net/qq_34274756/article/details/131031165