Comments in Python

Comments are non-executable statements in Python. It means neither the python compiler nor the PVM will execute them. Comments are intended for human understanding, not for the compiler or PVM. Therefore they are called non-executable statements.

There are two types of commenting features available in Python: These are single-line comments and multi-line comments.

Single-Line Comment

A single-line comment begins with a hash (#) symbol and is useful in mentioning that the whole line should be considered as a comment until the end of the line.

Example:
#Defining a variable to store number.
n = 50 #Store 50 as value into variable n.
  • In the above example program, the first line starts with the hash symbol, so the entire line is considered as a comment.
  • In the second line of code, "N = 50" is a statement and after the statement, the comment begins with the # symbol. From the # symbol to the end of this line, the line will be treated as a comment.

Multi-Line Comment

Multi-line comment is useful when we need to comment on many lines. You can also use single-line comment, but using multi-line instead of single-line comment is easy to comment on multiple lines.

In Python Triple double quote (""") and single quote (''') are used for Multi-line commenting. It is used at the beginning and end of the block to comment on the entire block. Hence it is also called block comments.

Example:
"""
Author: www.w3schools.in
Description:
Writes the words Hello World on the screen
"""

or

'''
Author: www.w3schools.in
Description:
Writes the words Hello World on the screen
'''

Docstrings

Triple double coat (""") and single coat (''') are actually docstrings, which are also used as comments. The key usage of docstrings is explained in the Python API Documentation chapter, and we will learn more about it in the further chapters.