如何在 Python3 中写评论?
注释是添加到程序中的文本注释,用于提供有关源代码的解释性信息。它们在编程语言中用于记录程序并提醒程序员他们刚刚对代码做了哪些棘手的事情,也有助于后代理解和维护代码。编译器将这些视为不可执行的语句。由于注释不执行,因此当您运行程序时,您不会在输出中看到任何注释指示。
句法 :
hash(#) 符号表示Python中注释的开始。
# This is a comment in Python
例子 :
python3
# This is the syntax of a comment in Python
print("GFG")
# Comments dont have any effect on the interpreter / output
python3
# This comment has no indent
def GFG():
# Since, this comment is inside a function
# It would have indent same as the function body
print("GeeksforGeeks")
for i in range(1, 2):
# Be careful of indentation
# This comment is inside the body of for loop
print("Welcome to Comments in Python")
# This comment is again outside the body
# of function so it wont have any indent.
print("Hello !!")
GFG()
python3
def my_function():
"""Demonstrates docstrings and does nothing really."""
return None
print("Using __doc__:")
print(my_function.__doc__)
print("Using help:")
help(my_function)
python3
# This function is adding two given numbers
def addition(a, b):
# storing the sum of given numbers in 'c'.
c = a + b
# returning the sum here
return c
# passing the value of a and b to addition()
a = 10
b = 3
sum = addition(a, b)
# printing the sum calculated by above function
print(sum)
python3
a = 12
if(a == 12):
print("True")
# elif(a == 0):
# print("False")
else:
print("Debugging")
输出 :
GFG
注释的缩进应该与它所注释的代码相同。
蟒蛇3
# This comment has no indent
def GFG():
# Since, this comment is inside a function
# It would have indent same as the function body
print("GeeksforGeeks")
for i in range(1, 2):
# Be careful of indentation
# This comment is inside the body of for loop
print("Welcome to Comments in Python")
# This comment is again outside the body
# of function so it wont have any indent.
print("Hello !!")
GFG()
输出
Hello!!
GeeksforGeeks
Welcome to Comments in Python
评论类型:
1. 单行注释:以“#”和空格开头的注释在Python中称为单行注释。这些注释只能延伸到单行,并且是Python中注释的唯一方式。例如
# This a single line comment.
2. 多行(块)注释:与其他编程语言不同, Python不支持开箱即用的多行注释块。但是我们可以使用连续的#单行注释来注释掉多行代码。块注释的一些示例-
# This type of comments can serve
# both as a single-line as well
# as multi-line (block) in Python.
3. 内联样式注释:内联注释出现在语句的同一行,跟在代码本身之后。通常,内联注释看起来像这样:
x = 3 # This is called an inline comment
a = b + c # Adding value of 'b' and 'c' to 'a'
4. 文档字符串注释: Python文档字符串(或文档字符串)提供了一种将文档与Python模块、函数、类和方法相关联的便捷方式。它在源代码中指定,用于记录特定代码段,如注释。与传统的源代码注释不同,文档字符串应该描述函数的作用,而不是如何。
例子 :
蟒蛇3
def my_function():
"""Demonstrates docstrings and does nothing really."""
return None
print("Using __doc__:")
print(my_function.__doc__)
print("Using help:")
help(my_function)
输出 :
Using __doc__:
Demonstrates docstrings and does nothing really.
Using help:
Help on function my_function in module __main__:
my_function()
Demonstrates docstrings and does nothing really.
评论的优点和用途:
- 计划和审查:在评论中,我们可以在编写源代码之前编写我们计划的伪代码。伪代码是自然语言和高级编程语言的混合体。这有助于更轻松地查看源代码,因为伪代码比程序更容易理解。
蟒蛇3
# This function is adding two given numbers
def addition(a, b):
# storing the sum of given numbers in 'c'.
c = a + b
# returning the sum here
return c
# passing the value of a and b to addition()
a = 10
b = 3
sum = addition(a, b)
# printing the sum calculated by above function
print(sum)
输出 :
13
- 调试:蛮力法是一种常用的调试方法。在这种方法中,在整个程序中插入打印语句以打印中间值,希望打印的一些值有助于识别错误。在进行调试之后,我们对这些打印语句进行注释。因此,注释也用于调试。
蟒蛇3
a = 12
if(a == 12):
print("True")
# elif(a == 0):
# print("False")
else:
print("Debugging")
输出 :
True