Comments clarify why certain code exists or how it works. They help others (and your future self) understand your code.
Use the #
symbol to add a comment for the rest of the line.
# This is a single-line comment
print("Hello, World!") # This comment follows code on the same line
Use consecutive #
lines or triple quotes (though triple quotes are mainly for docstrings).
# This is a multi-line comment
# explaining the following code block
print("Example")
Docstrings are special string literals used to document modules, classes, methods, and functions.
.__doc__
attribute or help()
def add(a, b):
"""
Adds two numbers and returns the result.
Parameters:
a (int or float): First number
b (int or float): Second number
Returns:
int or float: Sum of a and b
"""
return a + b
print(add.__doc__)
help(add)