Python | Comments

Python | Comments

In Python, Comments are used to explain or document code. They are ignored by the interpreter and do not affect the execution of the program.
There are two types of comment in Python:
1) single-line comments
2) multi-line comments

Single-line comment: This comment is start with the “#” symbol and continue until the end of the line.
For example:

# This is a single-line comment
x = 'ankit' 
#  x = 'rahul' 
# The above statement is also a single-line comment so this line ignored by the interpreter

Multi-line comment: This comment is also known as block comments, are used to comment out multiple lines of code at once. They start with a triple quotation mark (“””) and continue until the next triple quotation mark.
For example:

"""
As you can see
this is a
multi-line comment
"""

Another common way of writing multi-line comments is to use a single “#” symbol at the beginning of each line.

# This is a 
# multi-line comment

Note that using triple quotes is commonly used as a docstring also, which is a way of documenting the code (functions, classes, modules) and providing meta-information about the code.

Also, Python support inline comments using # , this is useful when you want to comment out a small piece of code and keep the rest of the code intact.

Leave a Reply

Your email address will not be published.