Register Login

Comments in Python

Updated Dec 08, 2020

In programming, every line of a program described the sequence of steps required to achieve an output. This output may be based on some input provided by the user. Some lines of the program may be easy and self-explanatory. But, some lines can be a little complicated such as a for loop or a function call. Someone else reading the code may find it difficult to understand. That is where comments come in handy.

Comments are used for specifying what a particular line means. This makes it easy for you to understand it when you read it later. Moreover, others can also read the comments and make sense out of your code.

In Python, you can write a comment after putting a # symbol before it. Let us look at the various ways to comment in Python.

Good and Bad Commenting in Programming

While coding, you can write comments after every line of the code if you want. But that is not necessarily useful. There is a difference between useful and useless comments. A comment can be called useful if it is short, simple and explains a particular line of code.

For example, look at the following comment:

word = "Python" # A variable word that has been assigned a string value of "Python"

This is not a very useful comment. This is because you can easily understand from the line of code that the variable word has a value of Python. There is no need for a comment here.

Check out this other example:

# An if statement checking whether the variable word is not present in the word_list 
if word not in word_list: 
    print(“Not found”)

As the if statement here is a bit complicated, the comment here clearly specifies its function.

In Python, there are two types of comments

  1. Single-line Comments
  2. Multi-line Comments

Single-line Comments

In Python, single-line comments are used for commenting out single lines of code. These comments start with a # symbol. The symbol is then followed by the text explaining the line of code. You can write these comments beside a line of code or above it.

Example

# Python program for defining a single-line comment
print('Hello World!')

Output

Hello World!

In the above example, the code block begins with a “#” symbol. This marks the beginning of the single-line comment. The python compiler skips this line. The only line that is executed is the print(‘Hello World!).

According to the Style Guide for Python Code (PEP8), the comments must not exceed 79 characters in per line. In case, your comment is exceeding this limit, it is advisable to break into multiple lines. For single-line comments, 70 to 72 characters are recommended.

Multi-line Comments

If you want to write a comment that spans multiple lines, multiline comments can be used. These comments may be used to describe a particular code block. Multiline comments can be declared using two ways. You can specify these comments using ''' (Triple single quotes) or """ (Triple double quotes). The quotes are mentioned at the beginning and at the end of the comments.

Let us look at an example:

"""
This is a multi-line comment
written in
more than just one line
"""

'''
This is another multi-line comment
written in
more than just one line
'''

print("Hello, World!")

 Comments in Python

 

How to Apply or Remove Comments From Programs Using the Python IDLE?

 

Commenting Single-lines or Multiple-lines of Code

 

The steps to comment out single lines of comments using Python IDLE are as follows:

 

  • Select the lines in the program you want to comment
  • Go to the Format tab in the IDLE
  • Select the Comment Out Region or press Alt+3
  • For commenting out multiple lines of code, follow the same steps

 

Comments in Python-2

 

 

 

Comments in Python-3

 

Uncommenting Single-lines or Multiple-lines of Code

 

Steps to uncomment lines of code are as follows:

 

  • Select the lines of code that have to be commented out
  • Go to the Format tab in the IDLE
  • Click on Uncomment region or press Alt+4

 

Comments in Python-4

 

 

 

Comments in Python-5

 

Conclusion

 

Comments are essential for increasing the readability of your code. This will be useful when you are sharing your code with other programmers. Adding comments is a useful skill and must not be overused. So, avoid putting comments after every line of code.

 


×