Python – Comments

The objective of adding comments to computer code is to make it easier to comprehend. It improves the readability of the code, making it easier to edit afterwards. When the compiler runs the code, it ignores comments. There are two ways to add a remark in Python – Comments.

 

  • Single line comment
  • Multi-line comment

Single line Comment

It begins with # and finishes with the line’s conclusion. Anything from # to the end of the line is a single line comment, and the compiler will disregard it.

 

Example:

The following example shows how to utilise single line comments in Python code. When building the code, the compiler ignores the comments.

 

# first line comment
print('Hello World!') # second line comment


 

The output of the above code will be:

Hello World!

Multi-line Comment

Although Python does not support multi-line comments, this may be accomplished by beginning each line with #. Enclosing text within tripe quotes (“””… “”” or “‘… “‘) has the same effect. The text included in triple quotes is treated as a remark by the compiler as long as it is not assigned to a variable.

 

Example:

Multi-line comments are utilised in the code below, which are done by commencing each line with a # or enclosing text within triple single quotes “‘…”‘.

 

#comment line 1
#comment line 2
'''
comment line 3
comment line 4
'''
print('Hello World!') 

 

The output of the above code will be:

Hello World!

Example:

Multi-line comments are utilised in the code below, which are done by enclosing content within triple double quotes “””…”””.

 

"""
comment line 1
comment line 2
"""
print('Hello World!')  


The output of the above code will be:

Leave a Reply

Your email address will not be published.