Python – 注释

Python – 注释

原文: https://howtodoinjava.com/python/python-comments/

在 Python(或任何其他编程语言)中,注释用于解释源代码。 注释描述了代码,这有助于将来维护应用。

# prints 4
print(2 + 2)

print(2 + 3)	# prints 5

"""
prints the sum of
two numbers which are 2 and 2
"""
print(2 + 2)

Python 中的注释类型

Python 支持编写简单的单行注释以及多行注释。

单行注释

一个简单的单行注释将以哈希(#)字符开头。 Python 运行时会忽略在#字符之后编写的任何文本,并将其视为注释。

# This is a simple comment
print(2 + 2)

print(2 + 3)	# prints 5

多行注释

Python 没有什么特别的东西可以写多行注释。 要编写它,我们可以编写多个单行注释

我推荐这种形式的注释。

# This statement
# prints the sum of
# two numbers which are 2 and 2
print(2 + 2)

我们可以利用未分配给变量的多行字符串字面值(使用三引号)。 Python 会忽略未分配给任何变量的字符串字面值,因此不会影响程序执行。

"""
This statement
prints the sum of
two numbers which are 2 and 2
"""
print(2 + 2)

将有关在 python 中编写注释的问题交给我。

学习愉快!