注释可用于提高代码的可读性。
在测试代码时,可以使用注释来阻止执行。
创建注释注释以 # 开头,python 将忽略它们:
实例
#this is a commentprint(hello, world!)
运行实例
注释可以放在一行的末尾,python 将忽略该行的其余部分:
实例
print("hello, world!") #this is a comment
运行实例
注释不必是解释代码的文本,它也可以用来阻止 python 执行代码:
实例
#print("hello, world!")print("cheers, mate!")
运行实例
多行注释python 实际上没有多行注释的语法。
要添加多行注释,可以为每行插入一个 #:
实例
#this is a comment#written in#more than just one lineprint("hello, world!")
运行实例
或者,以不完全符合预期的方式,可以使用多行字符串。
由于 python 将忽略未分配给变量的字符串文字,因此可以在代码中添加多行字符串(三引号),并在其中添加注释:
实例
"""this is a commentwritten in more than just one line"""print("hello, world!")
运行实例
只要字符串未分配给变量,python 就会读取代码,然后忽略它,这样就已经完成了多行注释。
以上就是python注释怎么创建的详细内容。
