带有示例的Python多行注释教程

Comments are used to put some text into the Python code. This text can be used to explain something about the application or some method data etc. Python provides two types of comments named Single Line and Multi-Line.

注释用于在Python代码中添加一些文本。 该文本可用于解释有关应用程序或某些方法数据的信息。Python提供了两种类型的注释,分别称为Single LineMulti-Line

单行注释 (Single Line Comment)

# is used to create a single-line comment. We will put # to the start of the line and the whole line will be interpreted as a comment and there will be no command execution.

#用于创建单行注释。 我们将#放在行的开头,整行将被解释为注释,并且不会执行任何命令。

# This is a single line comment

print("Hello Poftut.com")

#This is another  single line comment

print("Hello Poftut.com")  #This is a single line comment too

多行注释 (Multiline Comment)

Python also provides the multiline string which is triple quotes in our code. We will put triple quotes in order to start the multiline comment and put triple quotes to end the multiline string.

Python还提供了多行字符串,该字符串在我们的代码中是三引号。 我们将用三引号引起来以开始多行注释,并用三引号结束多行字符串。

"""

This is the comment

for multi line

Comment

"""

print("Hello Poftut.com")

We can also use strings in multiline comments start and end lines by providing comments.

我们还可以通过提供注释在多行注释的开始和结束行中使用字符串。

""" This is the start of the multiline comment

This is the body of the multiline comment

This is the end of the multiline comment"""

print("Hello Poftut.com")

多个单行注释 (Multiple Single Line Comment)

There is also an alternative way to create multiline comments in Python. We can use multiple single-line comments.

还有一种在Python中创建多行注释的替代方法。 我们可以使用多个单行注释。

# This is a single line comment

# This is another single line comment

# This is comment which will look like multiline comment

print("Hello Poftut.com")

注释码 (Commenting Code)

We can also comment on valid code. This will make the code just comment and the code line will not be executed. In this example, we will comment the print("Hello Poftut.com") line and there will be no output.

我们还可以对有效代码进行注释。 这将使代码只是注释,而代码行将不会执行。 在此示例中,我们将注释print("Hello Poftut.com")行,并且将没有输出。

# This is a comment

# print("Hello Poftut.com")

a = 5
LEARN MORE  SQL Comments Tutorial with Example
了解更多带有示例SQL注释教程

翻译自: https://www.poftut.com/python-multiline-comment-tutorial-with-examples/