One might wonder why comments are so popular among programmers and developers. Multiline comments in python programming reflects the way one thinks. How one took every single step towards solving a problem. Comments in a Python code represent one’s thought process. Also, it can be later used to understand the intention of your code.
By the use of comments, one allows an easier way to find errors and to fix them.
Also, with the help of comments, the code written today can be improved tomorrow. The same code is reused in different applications as well and this is all possible due to the use of comments.
The use of comments is very important to all kinds of projects, irrespective of their scale.
Commenting makes collaboration and understanding of code much easier. It also tends to increase the efficiency of the workflow.
Commenting while writing code is seen as a good practice among developers and programmers.
Without comments, things start getting so confusing that it reduces the readability of the code.
Though there are other ways of improving the readability of the code.
Like giving proper names to the variables, use of proper indentation, and defining various functions. But commenting just makes the most out of it.
In this article, we’ll try to cover the basics of comments and also how to write them in Python.
By the end of this article, you’ll have a clear idea about what, why, and how of commenting.
Why is the use of comments in your code Is Important?
Let us consider the situations in which consideration of the comments might come handy.
Code is read by you:
For instance, you are working for a client(A), developing a web service for them. You completed the whole project before the deadline and you are done with it.
But as a developer, you have a habit of commenting on your code at the end when the whole project is submitted. So you choose to put the comments in the written code a bit later.
Before you could do the commenting you put your hands on another project immediately. Within a few days, you will have completely forgotten about the client (A) project. And also that you were supposed to comment on the code.
Now six months later, Client (A) wants you to write some a patch for the previous code to add some new utility. Now when you open the text editor to write the patch, and the first thing that pops on your mind:
What did you even write??
Due to the absence of comments, you’ll just spend hours just parsing through your old code.
You will be completely lost in the mess, as you no longer remember the variable names.
So a scenario like this tells us that a developer/programmer must develop a habit of commenting on the code, before making the final submission.
Because it is too tiring to come back and comment on hundreds of lines of code at one go.
Code is read by fellow developers:
Now imagine a situation, where you are a back-end developer working on database fetching service. For an initial period of 3-4 months, you are the only developer on the team.
You understand your code completely, so you don’t use comments in the whole process.
As writing and maintaining comments takes time, you don’t see the point in commenting. And you like it that way.
Now the real problem arises after 3-4 months when new devs come into the team. And you are 10,000 lines deep into your project with zero comments.
So in order to avoid any of these situations, whether in a big or small project one must always comment on the code.
Writing Single/ multiline comments in python in Python:
Comments in Python can be written in two different formats.
- Single-Line Comments
- Multi-Line Comments
Single-Line Comments:
The single-line comments can be written in python which begins with a hash character(#).
Syntax:
#defining discount of 10% discount_10=0.90 #defining discount of 20% discount_20=0.80
Multiline comments in python:
There are two ways in which one can go about producing multi-comments in python.
Both ways are quite effective and useful.
Method 1:
One can simply use # to write multiple comments and create a comment block.
def commenting_101(): # This is an example # of how to comment using hash character # of multiple lines in Python
Method 2:
Another way of doing a multi-line comment in python is by wrapping the comments inside a set of triple quotes:a=5
b=6 c=a+b print(c) """ writing multiple line code in python can be super easy and can be fun """
That’s it for the tutorial on how to write multiline comments in python. Let us know how you feel reading the article and if it delivered value to you.