Python Comments

Python Comments are used to make a Python program readable or understandable to other people or for the one who writes the program.

In this tutorial, we shall learn how to write comments in a Python program.

In Python, a comment can span a single line or multiple lines. Based on the number of lines a comment spans, there are two types of comments.

Single Line Comments

‘#’ could be used at the beginning of a sentence that is intended to be a single line comment. ‘#’ inside a string literal is not considered the starting of a single line comment.

An example is shown below.

example.py – Python Program

number_1 = 5
number_2 = 6

# this is a comment
if number_1 * number_2 == 30: # this is also a comment # second hash doesn't do anything
    print("# This is not a comment as # is inside the string literal")
Try Online

Note : An empty String literal which is alone and contained in a line could work as a comment. But this is not a good practice.

Multiple Line Comment

”'<comment>”’ : multiple line comment enclosed between triple single quotes. “””<comment>””” : multiple line comment enclosed between triple double quotes.

example.py – Python Program

number_1 = 5
number_2 = 6

'''this is a comment
this is continuation of the comment
this comment has multiple lines'''
if number_1 * number_2 == 30:
    print("Comments Demonstration")
"""this is another way of expressing
multiple line comments"""
print("thank you")
Try Online

Conclusion

In this Python Tutorial, we have learnt to write single line comments and multiple line comments in a Python program.