Comments are brief descriptions of the logic and workings of the written code. They are used to enhance code readability.
Generally, two types of comments exist:
For single-line comments, approximately 70 to 72 characters are recommended. Such comments are generally used before or in front of a statement to explain only that one statement.
A single-line comment in Python starts with #
. The following is an example of a single-line comment:
# This is a single-line commentprint("Example of a single-line comment in Python")
Multi-line comments generally tend to explain a task and are more descriptive.
In Python, multi-line comments start and end with '''
. The following is an example of a multi-line comment:
'''This is an example of a multi-line comment.Multi-line comments are generally descriptivein nature and tend to explain a block of code.'''print("Example of a multi-line comment in Python")
The code below demonstrates the use of single and multi-line comments in Python:
"""Program to print the multiplication table of 5For instance 5 * 1 = 5"""for i in range (1,11): #The loop runs 10 timesprint('5 * ',i,' = ',5*i)# i iteratively increases by 1 everytime