Comments in Python

Python Tutorials

Summary: In this tutorial, we will learn what comments are in Python and why we should use them. We will also learn to write different types of comments such as single and multi-line comments in Python.

What is a Comment in Python?

Sometimes, in addition to programming language codes, we programmers may want to write statements that need not be executed, such as simple human-readable English that explain the logic of the code written.

These additional statements that are not executed as codes but remain in the program are known as comments.

The comment play no role in building the logic of the program, but helps other programmers to understand what does the code or the part of the program intends to do.

For example, the first line in the following code is a comment:

#adding numbers
sum = 5+6

How to write Comments in Python?

There are two ways to write comments in Python:

  1. Single line comment using # (hash character)
  2. Multi-line comment using multiples # or ''' (triple quotes)

Single Line Comment

To write a single line comment in Python, we should precede the statement with the # (hash character).

Here is an example that illustrates the same:

#check even number
if num%2 ==0:
    print("odd")

We can also write a comment following a python statement. In that case, it is called an inline single-line comment.

print("We are Programmers")   #This is an inline comment

Multi-line Comment

According to the block comment section of the PEP8 documentation, Python doesn’t have any mechanism to write multi-line comments.

Python has only one way to comment and that is by using the hash (#) character (as discussed before).

But, we can leverage the # character to write comments in multiple lines as follows:

#This is the first line
#This is the second line
print("We are learning Python programming.")

The documentation did mention the use of the ''' triple quotes, but that is only for writing the docstrings.

Triple quotes in Python are used for writing string literal that span multiple lines.

If we don’t assign them to any variables they may look like multi-line comments, but they are not really.

'''
This is a string literal,
we should not use it for writing comments.
'''
print("Sharing is Caring")

These strings are not ignored by the Python interpreter in the same way as the #comment, hence gets garbage collected.

The only exception to the garbage collection of string literals is when they are used as docstrings.

How to Comment Python code?

The comments in Python are not only for explanation or readability purposes, we can also use them to make codes un-executable.

For example, in the following program, we have commented the if statement to make it temporarily un-executable:

def divide(a, b):
    # if b==0:
    #     print("Division not possible")
    print(a/b)
    
divide(10, 2)   

Python interpreter ignores the statements preceding with #, whether the statement is Python code or anything else.

Commenting out Python code often helps in debugging.

Conclusion

The comments are those parts of the program that are ignored by the interpreter but remain to improve the readability of the codes.

We can write comments in Python by preceding the statement with the hash character (#).

The use of comments in Python is limited only by our imagination.

Leave a Reply

Your email address will not be published. Required fields are marked *