Comments are useful for understanding and maintenance of the code.
Single line comments
The first line is ignored by Python and the second line prints the string.
# Single line comment
print('Hello world')
Single line , inline and multiline Comments in Python for easy maintenance and understanding script
In-line Comment
Comment is used to the right of the code.
print('Hello world') # comment in same line
Multiline comments
Python ignores the string if it is not assigned to a variable, this technique is used to define a multiline comment. There is no direct support for multiline comment in Python.
Use three single quotes or three double quotes to create one multiline comment. Here only the string Hello world is printed.
'''
First line comment
Second line comment
third line comment
'''
print('Hello world')
Using three double quotes
"""
First line comment
Second line comment
Third line comment
"""
print('Hello world')
docstring in python
For a function or class we use documentation to understand the details about it, though this is written like we write comment but those can be accessed.
def add_num(a,b):
'''comment inside about the function'''
return a+ b
print(add_num(4,5))
print(add_num.__doc__)