我们如何在Python中创建多行注释?

When we need to comment on multiple lines/statements, there are two ways to do this, either comment each line or create multiline comments (or block comment).

当我们需要对多个行/语句进行注释时,有两种方法可以执行此操作,或者注释每行或创建多行注释 (或阻止注释 )。

In C / C++, we use /*...*/ for the multiline comment, but in Python programming language, we have two ways to comment a section.

C / C ++中 ,我们对多行注释使用/*...*/ ,但是在Python编程语言中,我们有两种方法来注释部分。

The first way is to comment on each line,

第一种方法是在每一行上注释,

This way can be considered as a single line comment in Python – we use the hash character (#) at the starting of each line to be commented.

这种方式可以视为Python中的单行注释–我们在要注释的每一行的开头使用井号( # )。

# This is line 1
# This is line 2
# This is line 3

And, the second way is to comment the section,

而且,第二种方法是评论该部分,

This way can be considered as a multiline comment in Python – we use triple single quotes (''') at the starting of the section to be commented and again triple single quotes (''') at the end of the second.

这种方式可以视为Python中的多行注释-我们在要注释的部分的开头使用三重单引号( ''' ),并在第二个末尾再次使用三重单引号( ''' )。

'''
This is line 1
This is line 2
This is line 3
'''

创建多行注释的Python示例 (A Python example for creating multiline comment)

'''
Function name: print_text
Parameters: None
Return type: None
Description: This function will print 
some text on the screen
'''

def print_text():
  print("Hello, world! How are you?")

if __name__ == "__main__":
  # Here, we will call the print_text function
  # that will print some text on the screen
  print_text()

Output:

输出:

Hello, world! How are you?

In the above program, there are two multiline sections which we is commented,

在上面的程序中,有两个多行部分,我们在其中进行了注释,

Section 1:
'''
Function name: print_text
Parameters: None
Return type: None
Description: This function will print 
some text on the screen
'''

Section 2:
# Here, we will call the print_text function
# that will print some text on the screen


翻译自: https://www.includehelp.com/python/how-do-we-create-multiline-comments-in-python.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值