Python教程–Python注释语法

前言

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

Python注释-Python教程

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)

原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/243734.html

(0)
上一篇 2022年4月11日
下一篇 2022年4月11日

相关推荐

发表回复

登录后才能评论