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 will learn different types of comments and how to write comments in Python language.
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
- Multiple Line Comments
Single Line Comments
A single line starts with hash symbol
#. Anything after
# in that line is considered a comment and ignored by Python interpreter.
‘#’ inside a string literal is not considered as a comment.
An example for single line comment is shown in the following example.
a = 5 b = 6 # this is a comment if a * b == 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 single line comment. But this is not a good practice.
Multiple Line Comment
We can use triple single quotes or triple double quotes for writing a multiple line comment in Python.
'''<comment>''' : multiple line comment enclosed between triple single quotes.
"""<comment>""" : multiple line comment enclosed between triple double quotes.
In the following program, we write multiple line comment using triple single quotes, and triple double quotes.
'''this is a comment this is continuation of the comment this comment has multiple lines''' """this is another way of expressing multiple line comments""" print("thank you")Try Online
In this Python Tutorial, we learned how to write single line comments and multiple line comments in Python.