Python Comments

A comment is a programmer-readable text in the source code of a computer program. The comment makes the source code easier for humans to understand. They are ignored by the compilers or interpreters while executing the source code. The syntax of comments is generally varying to different programming languages. Comments are a necessary thing for future development. It will help you write a clean and understandable code.
Python has two types of comments such as:                         

  • Single line comment
  • Multi-line comment

 

Single line comment is generally used as a quick comment with a short text. Whereas, Multi-line comment is used to describe the brief details of the code with a large text or for not executing some part of the code in the program.

Single line comment

A hash sign (#) is used as a single-line comment in Python. Python interpreter ignores to execute the code line after the hash sign till the end of the line, which is the part of the comment. It is the most commonly used comment in python.

print ("Hello, World!") # printing text
print ("Hello, Python!")

 

#Output
Hello, World!
Hello, Python!

Multi-line comment

To write a multi-line comment we use the triple-quoted string. Python interpreter ignores to execute the code lines which are in between the triple-quotes. It is used to comment on some code in a program, so that python interpreter does not execute that commented line of code.

'''
This is a multi-line
comment.
'''
print ("Hello, World!")
print ("Hello, Python!")
#Output
Hello, World!
Hello, Python!

Double quotes are also used to define the multi-line comment.

"""
This is a multi-line
comment.
"""
print ("Hello, World!")
print ("Hello, Python!")
Output:
Hello, World!
Hello, Python!

.     .     .

Leave a Reply

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

Python Tutorials