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 就会读取代码,然后忽略它,这样就已经完成了多行注释。

猜你喜欢

转载自blog.csdn.net/yxczsz/article/details/132564054