List of content you will read in this article:
Coding is a crucial task for running any application properly. Each developer has a different coding style and uses different methods and logic to do the same thing. But what if the code developed by one developer needs to be inspected by another developer with a different coding style? How will the developer get to understand the flow of the code? This is where commenting is essential to describe the motive of your code or explain the logic using comments.
Comments are simple text lines that do not get processed by compilers and interpreters. These are used only to understand the code better and improve readability. Developers get to know what the code is about; they can use it for themselves or for other developers’ consideration. Generally, it is a good practice to include comments while writing or updating the code, so there will be no confusion about the logic in the future.
Commenting sections within the code will highlight your thought process and help you to understand later on the purpose of your code. You can use comments on all kinds of projects, irrespective of their size. Without comments, things can be confusing. Comments are available for every programming language, but we are learning about using different types of comments in the Python programming language in this article.
Prerequisites for using comments in Python
Make sure you have installed Python3 and set up the environment on your system or server to proceed with the coding. If you do not have the prerequisite, then start with that first.
Why use comments in Python?
There are several benefits of using comments while coding in Python. We have mentioned some benefits of using comments that you should consider.
- Improves readability.
- Make the code easy to understand even by other coders with different coding styles.
- You can even understand the code later in the future.
- You can even include the source for the included or copied code.
- You can use the code fragment in another project if you know the context.
- It makes the code self-explanatory
- Comments also ensure that some coding parts will get ignored while testing.
In Python, comments start with a hash mark (#) and a space character. You can use the following syntax beginning with the (#) for using comments.
As comments are ignored while processing the code, you will not get an idea where the comment is in the code while getting the output.
Different types of Comments
Python supports three different types of comments. These are single-line, multi-line, and docstring comments. Each style comes with different syntax and usage. In this article, we have mentioned different types of comments that are commonly used as a good practice in Python.
- Single-Line Comments
These type of comments starts with the hash (#) character. Being a single line, anything that follows the # sign within a single line is considered a comment. As the next line starts, it is considered to be the code automatically. Below is the syntax available for using comment for the single-line comment.
# comments here
You can use two different ways of using a single-line comment in Python. You can use this type of comment either after or before the code as per your requirement.
As per the Python style guide, you can only use up to 79 characters in a single-line comment, as it is perfect for ensuring readability. If your comment characters exceed 79 characters, then it will fall under another type of comment, which is a multi-line comment that we will discuss next.
- Multi-line comments.
For writing more than a single line of comments, you can simply use the multi-line comment. You can include an entire block of comments that is supported by Python. You can use this type of comment for documentation purposes, such as explaining the flow of code, new changes, features added, etc. This will help you understand what the code was about without going through the entire code.
As Python does not come with explicit support for multi-line comments, you need to follow some steps before you can use it. To eliminate this issue, you can use the following method by using the hash character at the start of each line.
Another way of overcoming this issue you can use the string literals without assigning them to any variable. In this case, the line will be ignored by the Python interpreter and used as a comment line. You can leverage this feature to implement multi-line comments. You can either use (‘’) single quotes or double quotes (“”), as shown in the below image.
Another simple way of implementing multi-line comment is to use the (“””) three double quotes at the starting and include as many lines you want, then end it with (“””) three quotes, as shown below.
- Python Docstrings
Apart from commenting on a single line or multiple lines in Python, you can also comment on an entire module, function, class, object, method, etc., by using docstring. The docstring is available as the in-built feature, allowing you to associate documentation.
While writing lengthy codes, it is always necessary to include comments to understand the coding flow. It improves the readability of the code and lets another developer get an insight into your code and understand what the entire code is about. In Python, you can use three different types of comments that we have mentioned for your use.
It is a good coding practice always to include comments. To understand how different types of comments work and impact the output.
People also read:
I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.