Python的注释方法

程序代码中的注释并不是必须要写的,但是添加注释可以更方便的理解该行代码的意义。在 Python 中注释方式有两种,即 单行注释 和 多行注释

单行注释

单行注释以 # 开头,例如:

1
2
# 使用 print() 打印字符
print("Hello, World!")

多行注释

多行注释用三个单引号 ''' 或者三个双引号 """ 将注释括起来,也称之为 “三引号”。单引号或双引号本质上没有什么区别。例如

  • 使用 '''
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/python3 

'''
Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
'''

print("Hello, World!")
  • 使用 """
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/python3 

"""
Welcome to Python 3.6's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.6/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
"""

print("Hello, World!")
  • 三引号来做单行注释也是可行的
1
2
3
4
5
6
#!/usr/bin/python3 

""" Welcome to Python 3.6's help utility! """
''' Help on built-in function print in module builtins: '''

print("Hello, World!")
有钱任性,请我吃包辣条
0%