Comments act as signposts, making a piece of code self-evident and easy to read. Python commenting is a little different from other languages, but it’s not too difficult to get used to.
Comment in Python
There are two types of comments in Python: single line and multiple line. Single-line comments are useful for brief, quick comments (or debugging), whereas block comments are frequently used to describe something in greater detail or to block out an entire portion of code.
The hash (#) symbol is used in Python to denote the beginning of a comment line.
In Python, single-line comments are signified by the hash symbol “#” and do not contain any white spaces. As the name implies, this type of comment can only be one line long. They’re useful for short explanations and declarations of functions.
Consider the following scenario:
As you can see in the sample above, we placed comments at the beginning and end of the lines to meet our needs.
Multiline Python comments
Python allows you to have comments that span many lines. Multiline or block comments are examples of this type of comment. This commenting style can be used to explain anything more elaborate.
Examples of Multiline Python comments
This expanded type of comment can be applied to any or all of the following code. Here’s an example of how to utilize the Python multiline comment.
Using the hash sign (#)
You should start each line with the pound (#) sign, followed by a single space, to add multiline comments. A comment can be divided into paragraphs. Between each para, add an empty line with a hash mark.
The octothorpe is another name for the symbol (#). The word was coined by a group of Bell Labs engineers while working on the first touch-tone keypad project.
Using Quotes (Docstring Comments)
You can also use three single quotes before and after the part you want to comment on.
Docstring is a Python built-in feature that is usually connected with documentation. They are placed just beneath the functions, classes, or modules and indicate their purpose. The doc property in Python makes Docstring available.
The following is how to use Docstring in your code:
Benefits Of Python Comments
Python comments have a lot of benefits. Consider the following scenario: your senior developer has requested that you make some last-minute tweaks to a function in your code. You feel a little anxious since you can’t recall whether or not you added remarks.
However, once you open your codebase, you’ll be relieved to discover that Python comments have come to your rescue!
The advantages of comments in Python are numerous, thus they should be used wherever possible. Take a look at a few of them:
Python comments make the code much easier to understand. They simplify the code and eliminate the need to remember why specific code blocks were written. As a result, Python comments not only make it easier to read your own code from six months ago, but they also help other developers understand your work.
- Blocking out code for testing or execution
Another area where comments are extensively utilized is in the process of blocking out code for testing or execution. You can always comment out the remaining lines of code the next time you want to test out certain specific lines of code.
Do you want to learn how to make good python comments? Then take the following steps:
- Always use comments to explain what a code block performs in broad terms rather than in specifics.
- Only make comments when they’re absolutely essential. To put it another way, get rid of any superfluous comments that may cause confusion in your codebase.
- Keep them brief and straightforward. This ensures that anyone can easily read them and that they do not lose time trying to comprehend them.
A program’s value is enhanced with comments and docstrings. They improve the readability and maintainability of your programs. Even if you need to refactor the same code later, having comments available will make it easier.