- python 语法
- python 变量
注释可用于解释 python 代码。
注释可用于提高代码的可读性。
在测试代码时,可以使用注释来阻止执行。
创建注释
注释以 # 开头,python 将忽略它们:
实例#this is a comment print("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 line print("hello, world!")
运行实例
或者,以不完全符合预期的方式,您可以使用多行字符串。
由于 python 将忽略未分配给变量的字符串文字,因此您可以在代码中添加多行字符串(三引号),并在其中添加注释:
实例""" this is a comment written in more than just one line """ print("hello, world!")
运行实例
只要字符串未分配给变量,python 就会读取代码,然后忽略它,这样您就已经完成了多行注释。
- python 语法
- python 变量
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!