Comments are non-executable statements. It means neither the Python compiler nor the PVM will execute the comments. Main purpose of writing comments is to make understandable to human beings about the code and not for Python compiler or PVM.
In Python, comment is used to explain the code. It is used to make code more readable. Writing comments make our programs clear to others.
Types of Comments in Python
- Single Line Comments
- Multi Line Comments
Single Line Comments
Single line comment is start with a hash(#) symbol. The statement start with hash(#) symbol till the end of the line treated as comment.
#This is a single line comment print("Hello, World!")
Here, the first line with hash(#) symbol is comment line. and the second line, print(“Hello, World!”) is a statement.
Multi Line Comments
When we want to have more than one line comment, it becomes tedious job to mark every line with single line comment with hash(#) symbol.
In order to use multi line comments, we can use comment line inside triple double quotes (“””) or triple single quotes(”’) in the beginning and ending of the comment statement block.
""" This is a multi line comment which make non-executable to more than one line. """ OR ''' This is a multi line comment which make non-executable to more than one line. '''
Although, in python there is only single line comment supported. Multi line comments are not available in python.
Comments inside the triple quotes are regular strings with the exception that they can span multiple lines. That means memory allocated to these strings internally. If these strings are not assigned to any variable, then these are removed from memory by the garbage collector and hence can be used as comments.