How to write comments in Python

Comments in python are parts of source code which aren’t executed by the python interpreter. Comments contribute no functionality to the application program and have no meaning for users of the application program. But comments are of great help for the programmers. Comments increase the readability and understandability of the source code and help the programmers in refactoring or debugging the code and while adding source code for new features in the application program. In this article, we will see how to write comments in python using different ways.

How to write single line comments in Python?

We can write a single line python comment using # symbol. In python, anything written after # till a line break including the symbol itself is considered as a comment. Whenever a line break is encountered, the single line comment terminates.We can start a single line comment anywhere in the program using # symbol and whole statement after the symbol becomes a comment.

Following is an example code in which a single line comment is  written inside a python dictionary.

myDict={"name":"PythonForBeginners.com",
        #This is a single line comment inside a python dictionary
        "acronym":"PFB"
        }
print(myDict["acronym"])

We can also start a single line comment after a python statement.

myDict={"name":"PythonForBeginners.com",
        #This is a single line comment inside a python dictionary
        "acronym":"PFB"
        }
print(myDict["acronym"]) #This is single line comment which starts after a python statement

How to write multi line comments in Python?

Theoretically, python doesn’t have a syntax for multi line comments but we can use other ways like multi line strings and single line comments  to write multi line comments in python.

We can write multi line comments in python using single line comment by writing single line comments on consecutive lines as shown in the following example.

#This is multiline comment
#written using single line comments
#for a demonstration
myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"])

We can also start multi line comments in python after a python statement while implementing multi line comments with # symbol because it necessarily behaves as a single line comment.

#This is multiline comment
#written using single line comments
#for a demonstration
myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"]) #This is multiline comment after a python statement
#written using single line comments
#for a demonstration

To write multi line comments in python, we can also use multi line strings. If we do not assign a multi line string to any variable, the multi line string will be parsed and evaluated by the interpreter but no byte code will be generated for the multi line string because no address can be assigned to them. Effectively, the multi line strings will work as multi line comments. Following is an example of multi line comment written using multi line strings.

"""This is multiline comment
written using multi line strings
for a demonstration"""
myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"])

We have to keep in mind that multi line comments when written using multi line strings are not true comments and they are just unassigned string constants. For this reason, they should follow proper indentation and can be started only from a newline. If we try to write a multi line comment using a multi line string after any python statement, it will cause syntax error.

myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"])"""This is not a multiline comment and 
will cause syntax error in the program"""

Conclusion

In this article, we have seen how to write comments in python using # symbol and multiline strings. Stay tuned for more articles.

Leave a Reply

Your email address will not be published. Required fields are marked *