A comment is a piece of code that isn’t executed by the compiler or interpreter when the program is executed. Comments can only be read when we have access to the source code. Comments are used to explain the source code and to make the code more readable and understandable. In this article, we will see how to write single line and multi line comments using different methods in python.
What is a single line comment in python?
Single line comments are those comments which are written without giving a line break or newline in python. A python comment is written by initializing the text of comment with a #
and terminates when the end of line is encountered. The following example shows a single line comment in a program where a function is defined to add a number and its square to a python dictionary as key value pair.
#This is a single line comment in python
def add_square_to_dict(x,mydict):
a=x*x
mydict[str(x)]=a
return mydict
We can also add a single line comment after another statement.
#This is a single line comment in python
print("Pythonforbeginners.com") #This is also a python comment
What is a multi line comment?
As the name specifies, a multi line comment expands up to multiple lines. But python does not have syntax for multi line comments. We can implement multi line comments in python using single line comments or triple quoted python strings.
How to implement multi line comments using # sign?
To implement multi line comments using #
sign, we can simply depict each line of a multi line comment as a single line comment. Then we can start each line by using #
symbol and we can implement multi line comments.
#This is a multiline comment in python
#and expands to more than one line
print("Pythonforbeginners.com")
When writing multi line comments using #
symbol, we can also start multi line comments after any python statement.
#This is a multiline comment in python
#and expands to more than one line
print("Pythonforbeginners.com") #This is also a python comment
#and it also expands to more than one line.
How to implement multi line comments using triple quoted strings?
Multi line strings in python can be used as multi line comments if they are not assigned to variables. When the string isn’t assigned to any variable, they are parsed and evaluated by the interpreter but no byte code is generated because no address can be assigned to the strings. Effectively the unassigned multi line string works as a multi line comment.
"""This is
a
multiline comment in python
which expands to many lines"""
Here we have to keep in mind that the multi line comments are only string constants that have not been assigned to any variable. So they have to be properly intended unlike single line comments with #
symbol so that syntax errors can be avoided.
Also, multi line comments which use triple quotes should always start with a newline which is not the case for a single line comment.
#This is a multiline comment in python
#and expands to more than one line
"""This is
a
multiline comment in python
which expands to many lines"""
print("Pythonforbeginners.com") """This is not
a
multiline comment in python
and will cause syntax error"""
Conclusion
In this article, we have seen how to write single line and multi line comments in python. We have also seen how to write multi line comments using strings. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.