在 Python 中,单行注释使用“#”符号,多行注释使用三个单引号(''')或三个双引号(""")括起来的块。注释通常用于解释代码的目的,提供对代码的描述、解释、文档等等。
以下是一些快速注释 Python 代码的方法:
单行注释:在需要注释的代码行前加上“#”符号。
# This is a comment
print("Hello, World!")
多行注释:使用三个单引号(''')或三个双引号(""")括起来的块。
'''
This is a
multi-line
comment.
'''
"""
This is another
multi-line
comment.
"""
快速注释:选中要注释的代码块,按下 Ctrl + /(Windows、Linux)或 Command + /(macOS)快捷键,即可在所选行前面添加“#”符号进行注释。
# This is a comment
print("Hello, World!")
# This is another comment
print("Goodbye, World!")
使用注释是一种很好的代码习惯,可以帮助其他人更好地理解代码。