How to Comment in Python

Photo of author

By Vijay Singh Khatri

While writing codes in Python, there are instances when you need to explain your thought process behind that code to make other people understand what your intention is. Even sometimes you need to keep a note to make yourself understand later why you’ve written it. This process of keeping notes is called commenting on Python. It helps you to easily identify the errors, fix them, and improve your code in the future in case required. No matter what project you’re working on, commenting is important to maintain a seamless workflow. 

How to Comment in Python?

In this article, we are going to explain the methods to comment in Python coding. Before we get into the commenting section, let us take a look at what Python is and its coding process in short.

What is Python?

Python is a programming language introduced in the year 1991 by Guido Van Rossum. This programming language is used to create software, develop server-side web apps, connect with databases for reading and modifying data, handle big data, and execute complex mathematical operations.

Python has simple syntax (like the English language). If you’re programming using Python, you’ll require an operating system to start with. Since Python is cross-platform, you can use it to write codes on Windows, macOS, and Linux.

Writing Comments in Python

As mentioned above, Comment is helpful for a programmer to understand and make a note if needed while typing codes. Writing a comment in Python is a straightforward process, which uses the ‘#’ hash mark at the beginning of the words. 

Here are some examples of Python comments: 

Example: #this is an example Comment

Whatever the code may be, Python will never execute the comment. The comment is written after #(hash mark).

Example: print (“Hello, welcome!”) # This is a comment

The above code will only print “Hello, welcome” as output, and it ignores whatever we mentioned after # as any text preceded by # is considered as a comment in Python code. 

Types of Comment

There are two types of comments used in Python and they are discussed as follows:

  • Single-line Comment

A single-line comment is created using # initially and then stopping at the same line. Make sure to use fewer words otherwise it won’t be a single-sine comment. 

Example: #This is a single comment 

  • Multi-line Comment

Unlike other languages like Java, C, C++, etc., Python follows a different way to mention multi-line comments in its code.

Let us see how multi-line comment is added in Java code:

Java : /* multiline comment

Used in Java is

Very simple */

Here it is clearly shown that in Java, multi-line comments are simply added between /* and */. 

In Python, if we write multi-line comment the same way we used in the single-line comment, it will generate an error:

Example:

# this is the multi-line Comment

Using Python, which will show 

An error 

By default, the 1st line will be treated as a single-line comment. However, a syntax error will be shown for other lines.

There are two methods to add multi-line comments in Python. One is as simple as the single-line comment, which is by entering # at the beginning of each line. 

Example:

def multiline_comment():

# this is the multi-line Comment

# that is used in Python

# as a simple method

Here, every line mentioned above is started with # (hash mark), on which all the comments will be ignored while executing the program without an error.

Instead of typing # at the beginning of every line, another method that you can use to add a multi-line comment is binding the comments within triple quotes (“””).

Example: 

“””

It is the multi-line comment used in Python

That one can use to describe the code in detail

For reference.

“””

Shortcuts for Adding Comments in Python

Using # (hash mark) to add comments may increase the overall time for writing code. So, we are going to discuss some tricks to speed up the process of adding comments to Python code.

  • The first out of those tricks is using multiple cursors to make more than one selection at the same time. This process is mostly used with PyCharm. Multiple cursors can be done by simply holding the Ctrl or Cmd button on the keyboard and clicking the left mouse button on every place where you need to add comments and type comments for multiple lines.
  • Instead of clicking every line of comments, you can go with toggle comments. That is executed by simply selecting the particular codes and pressing Ctrl + / on a Windows PC and Cmd +/ on a  Mac computer. It will add # (hash mark) in front of all selected code or text and make it a comment line.
  • If the comments are too long, you can click on the small down arrow, which is present on the left side of the comment, to collapse the comments. This method is used to hide long comments.

Conclusion

Python comment is one of the easiest ways to explain your codes in an easy-to-understand way. However, you need to be more conscious of how & where it is being used for making programs easier. No matter whether you’re a beginner or a professional already, Python comments will help you to do your job more precisely. Hope, this article has addressed how to execute python comment in detail for you.

Leave a Comment