Python single-line, multi-line comment symbol usage and specification

Original url: http://www.iplaypython.com/jichu/note.html

There are many kinds of comments in python, including single-line comments, multi-line comments, batch comments, and Chinese comments are also commonly used. Python comments also have their own specifications, which will be introduced in the article. Comments can play the role of a note. When working in a team, the code written by an individual is often called by multiple people. In order to make it easier for others to understand the passage of the code, using comments is very effective.

1. Python single-line comment symbol (#)

The pound sign (#) is often used as a single-line comment symbol. When you use # in the code, any data to the right of it will be ignored and treated as a comment.
print 1
#Output the content to the right of 1# will not be output during execution.

2. Batch, multi-line comment symbols

In python, there are also many lines of comments. In this case, batches of multi-line comment characters are required. Multi-line comments are enclosed in triple quotes ''' ''', for example:
python multiline comment
Some people may say that this is not confused with multi-line strings, but many programmers I have seen use it like this, when using Just be careful to differentiate.

Three, python Chinese annotation method

When writing code in python, it is inevitable that Chinese will appear or be used. At this time, you need to add Chinese comments at the beginning of the file. If you don't declare the format of the save code at the beginning, then it will save the file with the ASKII code by default. If there is Chinese in your code, an error will occur, even if your Chinese is included in the comments. So it is very important to add Chinese annotations.
#coding=utf-8
or:
#coding=gbk
Tip: Both of the above can represent Chinese comments. I have seen more people using urf-8.

A good programmer, adding comments to the code is a must. But make sure that the comments are all important things, like you can see what they are doing at a glance, and useless code does not need to be commented.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325570702&siteId=291194637