‘What are comments’ is one of the most crucial questions with regards to python. Comments refer to lines that exist in computer programs that the interpreters and compilers ignore. Furthermore, putting comments in programs increases the readability of a code for humans.
How to Put Comments in Python
Python is one of the popular programming languages. In Python, there are two ways to putting comments or annotating the code.
The first way of putting comments or annotating code is to include comments that indicate or detail what exactly a section of code – or snippet – does.
The second way of putting comments or annotating code involves the use of multi-line paragraphs or comments that serve as documentation for others that read one’s code.
One can consider the first type as a comment for oneself, and the second as a comment meant for others. Most noteworthy, there is not a right or wrong way to add a comment and one can do whatever feels comfortable.
The creation of single-line comments takes place simply by beginning a line with the hash (#) character. Moreover, their automatic termination takes place by the end of the line.
For example:
#This would be a comment in Python
The use of comments that span multiple lines takes place to explain things in more detail. Moreover, their creation takes place by adding a delimiter (“””) on each end of the comment.
“””
This would be a multiline comment
in Python that spans several lines and
describes your day, your code, or anything you want it to
“”” .
Browse more Topics Under Variables, Expressions and Statements
- Values, Variables and Keywords
- Operators and Operands in Python
- Operator Precedence
- Expressions and Statement
- Taking Input and Displaying Output
Python Commenting Shortcuts
It can certainly be a tedious task to type out all the hash marks every time one has a need to add a comment. So, what can one do with regards to putting comments in python to speed things up? Below are a few tricks to speed things up when commenting.
One of the first things one can try is using multiple cursors- placing more than one cursor on the screen for the purpose of accomplishing a task. The way to do it is to hold down the Ctrl key while doing left-click, and the blinking lines on the screen would appear. This is most effective when one has to comment on the same thing in several places.
However, sometimes there may be a long stretch of text that needs to be commented out. In such a case, clicking each and every line to comment would take a huge amount of time. As such, one can toggle comments, selecting the desired code and pressing Ctrl+/.
Consequently, all the highlighted text will be ignored by the program and prepended with a hash mark.
In case the comments in a script are really long, an option may come from the text editor to collapse them by making use of the small down arrow on the left-hand side.
In order to hide the comments, one must simply click the arrow. Furthermore, this works best with long comments that are spread out over multiple lines or docstrings that take up most of the program’s start.
Combining these tips will make commenting on the code quick, easy, and painless!
FAQs For Putting Comments
Question 1: Explain the benefits of putting comments in python?
Answer 1: The benefits of putting comments in python are as follows:
- Comments help in the explanation of python code.
- Comments increase the readability of a code.
- Comments help in the prevention of execution when testing code.
Question 2: What are the various types of comments in python?
Answer 2: There are two types of comments in python. Furthermore, these two types are single-line comments and multi-line comments.
Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I also love your website because all type of information is available in your blogs. You made my day. Thanks you for everything. I have bookmarked more article from this website. Such a nice blog you are providing.