Comments are the lines in a computer program that help to build a better understanding of the functionality of the system. In any programming language, comments are written in natural language and in a human-readable way. They are not executed rather they are ignored by the compiler or interpreter. The comments in Python are used for various purposes. In this article, we will explain to you how you can add comments in python.

Benefits of using comments in Python

The following are the benefits of using comments in Python:

Comments make code more readable: While developing a computer program, the programmer writes multiple classes and thousands of lines of code. Sometimes, the programmer forgets why a certain piece of code is written. The comments make the code more readable and help us to remember why a piece of code is added and what its intended purpose is.

Comments enhance the code understandability: Comments help us to understand why a certain code block is added in the program and what its purpose is. If a programmer writes a block of code, then he may not add the comments because he understands the purpose of writing a specific block of code. But, if another programmer wants to update the code, then it is not so easy for him to understand the code in minutes. So, when we add the comments, it helps other programmers to understand the code. Let’s assume, you are working on the Python project. It was the medium size project initially. But for now, your company to enhance this project and bringing more developers and programmers to write the code. If you have no written the comments in your program, then you will see that it will be very hard for them to understand the code and it will take more time. But if you have properly written the comments with every block of code, then the newly hired developers will easily understand the existing code. Initially, writing and maintaining the comments take some time, but it saves a lot of your time in the future.

Comments promote the code reusability: When we develop similar software systems, then reusing the existing is the best practice. It saves a lot of our effort and time. While reusing the code, first we check what components we have to reuse. We have to understand the functionality of the particular component. Comments help us to understand the functionality of the component and they promote the code re-usability.

Writing comments in Python

Writing the comments in Python is very easy. The comments in Python start with the # mark. In python we use three types of comments:

  1. Single line comments
  2. Inline comments
  3. Multi-line comments

Single line comments

The single-line comments are written shortly in a single line. Single line comments convey the purpose of writing a specific function or a line of code. In a simple program the comment is like this:

Python Comments Python

Output

The output is printed on the Python console. In the output, only “Hello World” is printed. The comment is ignored by the compiler.

In a for loop, the comments are like this

Python Comments Python

Inline comments

The inline comments are written at the end of the line of code. In a simple “Hello World” program, the inline comment is like this

Python Comments Python

The inline comment in a for loop is like this:

Python Comments Python

Multiline comments

Python does not provide a distinct way to write the multi-lines comments. We can write multiline comments in Java, C, C , and C# but in Python, there is no separate way to write multi-line comments. The multiline comments are generally used to explain complex block of code. In multiline comments, each line starts with # mark. If we want to write multiple lines of comments, then each line will start with a # mark like this:

Python Comments Python

Conclusion

Comments are very useful in the Python program. It helps us to understand the program. It makes the Python programs more readable for humans. Comments promote the reusability of existing programs. In this article, we have seen three ways of adding comments in Python. By using these three ways, you can make your python programs more readable, understandable, and reusable.

About the author

Python Comments Python

Kamran Sattar Awaisi

I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems.