Comments in programming are the most useful text chunks which provide brief information about the particular code. You can use comments in your code to help with documentation. It will keep someone informed about what you have written. The concept of comments in programming can help the dynamic working teams to understand what code has been written by previous teams and much more. Now, we are here to talk about the comments in python and along with this, we can talk everything about comments. Let’s Roll!!!


Why you should use comments while coding?

You can use comments for various purposes. It keeps your code clean, readable, and well documented. I have stated some applications of the comments in coding.

  • For Planning and reviewing your code :

Yes, as the name suggests, you can write the pseudocode using the comments before writing the source code. This planning will not only help in documenting but also your source code becomes much easier to read.

  • Description of your code :

While coding, you can make use of comments to write a description of the working of code or what can you expect from the particular line of code. We can see the examples in the later stages of the article.

  • Resource management :

While coding, you can insert an ASCII character, logos, and flow charts for better understanding or any particular purpose. But, the best thing is you can format them as comments.

  • For Debugging :

In the process of coding, the print statements are used at regular intervals to print the values to identify the errors occurring. Later, by using comments, you can negate those print statements.

  • Makes work easier :

In coding, the major stress will be finding the bugs and not able to understand the previously written codes by someone. Developers spend most of the time in this phase. But using comments to describe, document, and debug the code can make work easier and faster as well.


How to comment on python code?

Every language has the commenting option to enhance the code quality, readability, and documentation purposes. Python is not an exception here. You can make comments in python. Before we do so, you should know about different types of comments in python. So, let’s discuss each one of them with examples. 

Single line comments in python

The basic feature of the single-line comments in python is they will start with “#” (Hashtag) without any space or whitespace. Note that when the comment exceeds one line, you have to use another hashtag in a new line. You can make use of single-line comments specifically for short descriptions, short notes, function declarations, and more.

#This is my first python comment
#Print "Hackanons" in the output 
print("Hackanons")
Hackanons 

As you see, you can use single-line comments to make short descriptions, and also you can write what the particular line of code will do.

Multiline comments in python

The multiline comments in python can be seen as the text enclosing between a set of delimiters – (” ” “). In the single-line comments, you will use # before the text. But here, you have to enclose your text between a couple of delimiters as shown. You should leave any space between the delimiters. The multiline comments will come into the party when the single-line comments are not enough to convey the meaning.

"""
This is my first python multiline comment.
I just saw an article which explains the comments in python.
There I learnt how to use multiline comments in python. 
Now I am printing, what I have learnt today.
"""
print("Today, I learnt about multiline comments in python")
Today, I learnt about multiline comments in python

You can use this comment type to describe in detail. You can add as many lines you want.

Python Docstring comments

Docstring comments are one of the most useful comment types in python. This comment should be added below the function, class, or methods. Then you can later call it using the _doc_ attribute in the print statement. Let’s see how it works.

def add(a,b):
    """Adds a with b"""
    return a+b

print(add.__doc__)
Adds a with b

Wow!. You can see the amazing in-built comment type doing work for you.

Let’s put it all together!!!

Till now, in python, we have come across three types of comments. Assuming that, you have no doubts about all those, I am moving a bit forward and using all three comments in a single code. So that, you will get a clear picture of the working of code comments.

"""
Over the past 10 minutes, we have learn about comments in python.
We have gone through 3 types of comments and thier usage.
Now, it's time to put it altogether. 
Let's roll!!!
"""
def substract(a,b):
    """Substrcts value of a with value of b"""
    return a-b

#Print the docstring comment output 
print(substract.__doc__)
Substracts value of a with value of b

Wrapping Up

Comments are of the most useful aspects of programming. Python supports 3 types of comments. Single, multiline, and docstring comments. We have gone through all 3 types of comments with illustrations. Comments in python add more value to the code quality, documentation, debugging, and readability as well. That’s all for now, next time – another topic. Happy python!!!

Categorized in: