python notes: #004#comment

Notes

Target

  • The role of annotations
  • single line comment (line comment)
  • Multi-line comments (block comments)

01. The role of annotations

Use the language you are familiar with to annotate some codes in the program to enhance the readability of the program

02. Single line comment (line comment)

  • #Starting with , #everything on the right is treated as explanatory text, not the actual program to be executed, and only serves as an auxiliary description

  • The sample code is as follows:

# 这是第一个单行注释
print("hello python")

In order to ensure the readability of the code, #it is recommended to add a space first, and then write the corresponding description text.

One-line comments added after code

  • During program development, it is also #possible to add descriptive text after (beside) the code
  • However, it should be noted that in order to ensure the readability of the code, there must be at least two spaces between the comment and the code

  • The sample code is as follows:

print("hello python")  # 输出 `hello python`

03. Multi-line comments (block comments)

  • If you want to write a lot of comment information, one line cannot be displayed , you can use multi-line comments
  • To use a multi-line comment in a Python program, use a pair of three consecutive quotes (both single and double quotes)

  • The sample code is as follows:

"""
这是一个多行注释

在多行注释之间,可以写很多很多的内容……
""" 
print("hello python")

When do you need to use annotations?

  1. Comments are not as many as possible . For clear code, there is no need to add comments
  2. For complex operations , several lines of comments should be written before the operation begins
  3. Comments should be added at the end of the line for code that is not self -explanatory (for readability, comments should be at least 2 spaces away from the code)
  4. Never describe the code, assume the person reading the code knows more Python than you, he just doesn't know what your code does

In some formal development teams, there is usually a practice of code review, that is, a team reads each other's code

About Code Specifications

Programmers of any language, writing code that conforms to the specification is the first step in starting a programming career

Guess you like

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