Are Python comments optional? Make it clear this time

Hi everyone, hello! I am a cheese who loves to touch fish ❤

insert image description here

Comments

Used to prompt or explain the role and function of some code to the user,
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.
At the same time, when the work is handed over and the code continues,
let the next person understand what you are doing.

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 self-documenting,
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.


Please add a picture description

Python single-line comments

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

#注释内容


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("人生苦短,我用python")
print("这里本来要放网址演示,但是平台审核不给过")
#使用 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("http://c.biancheng.net/python/")  #输出Python教程的地址
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 a comment appears as part of a string,

can no longer treat them as comment tags,

Instead, it should be seen as part of the normal code,

For example:

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

operation result:

Hello,World!
#是单行注释的开始

For the first two lines of code,

Python does not see the three quotes here as a multi-line comment,

Instead, think of them as the beginning and end of the string.

For line 3 of code,

Python also doesn't treat the pound sign as a single-line comment,

Instead, treat it 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 useful function,

It is used to debug the program.

for example,

If you think there might be a problem with a piece of code,

You can comment this code first,

Tell the Python interpreter to ignore this code,

Then run it again.

If the program can execute normally,

Then it can be explained that the error is caused by this code;

Conversely, if the same error still occurs,

Then it can show that the error is not caused by this code.

Using comments in the process of debugging a program can narrow down the scope of the error,

Improve the efficiency of debugging programs.

insert image description here

Guess you like

Origin blog.csdn.net/m0_74872863/article/details/129989092