It is also important to learn to add necessary comments to the program. Comments can not only be used to explain the role and function of certain parts of the program (describing the function of the code in natural language), but can also temporarily remove the code when necessary, which is a good helper for debugging the program.
Of course, the biggest effect of adding comments is to improve the readability of the program! Many times, I would rather write an application myself than improve other people's code. The lack of reasonable comments is an important reason. Although good code can speak for itself, we never know who will read this code in the future and whether he or she will have the same thoughts as you. Or after a period of time, you may not know the purpose of writing this code at that time.
Generally speaking, reasonable code comments should account for about 1/3 of the source code. The Python language allows null characters or comments to be inserted anywhere, but not between identifiers and strings.
There are two forms of comments in Python source code, namely single-line comments and multi-line comments: Python uses the pound sign (#) to indicate the beginning of a single-line comment, and the code following the "#" sign until the end of the line is will be ignored by the interpreter. A single-line comment is to comment a line of code in the program. In a Python program, just put the pound sign (#) before the content that needs to be commented. Multi-line comments refer to commenting out multiple lines of code in the program at one time. In Python programs, use three single quotes or three double quotes to enclose the content of the comment.
Single-line comments and multi-line comments are added to the following code:
#这是一行简单的注释 print ("Hello World!") ''' 这里面的内容全部是多行注释 Python语言真的很简单 ''' # print("这行代码被注释了,将不会被编译、执行!") """ 这是用三个双引号括起来的多行注释 Python 同样是允许的 """
These comment parts in the above program have no impact on the program itself. The main function of the comment content is to "show others" and show people Provide some explanatory information, and the Python interpreter will ignore these comments.
In addition, adding comments is also an important way to debug the program. If you think there may be a problem with a certain piece of code, you can comment out the code first, let the Python interpreter ignore the code, and compile and run it again. If the program can be executed normally, it means that the error is caused by this code. This narrows the scope of the error, which is helpful for debugging; if the same error still occurs, it means that the error is not caused by this code, and it also narrows the scope of the error.
Related recommendations: "Python Video Tutorial"
The above is the detailed content of How to comment multiple lines of code at the same time in python. For more information, please follow other related articles on the PHP Chinese website!