Comments in Python are non-executable statements. It means neither the Python compiler nor the PVM (Python Virtual Machine) will execute them. Comments are intended for human understanding, not for the compiler or PVM. Therefore they are called non-executable statements. Two types of commenting features are available in Python: single-line and multi-line comments.



Single-Line Comment

A single-line comment begins with a hash (#) symbol and helps mention that the whole line should be considered a comment until the end.

Example:

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

Multi-Line Comment

A multi-line comment is helpful when we need to comment on many lines. You can also use a single-line comment, but using a multi-line instead of a single-line comment is easy to comment on multiple lines. In Python, Triple double quotes (""") and single quotes (''') 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, and you'll learn more about it in further tutorials.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram