Python Certification Training for Data Scienc ...
- 68k Enrolled Learners
- Live Class
Have you ever asked any programmer to explain the code he wrote a year ago? Or have you ever tried reading your own previously written code? It would become very time consuming and tiring if you had to reanalyze every block of code from scratch! The best thing to do is, add a comment. Comments are not something just to increase the lines of your code but it is the best way to make your code meaningful. Here is a complete guide to help you understand all about Comments in Python.
A comment, in general, is an expression of one’s ideas. In programming, comments are programmer-coherent statements, that describe what a block of code means. They get very useful when you are writing large codes. It’s practically inhuman to remember the names of every variable when you have a hundred-page program or so. Therefore, making use of comments will make it very easy for you, or someone else to read as well as modify the code.
Comments are very important, but you will need to know how to make use of them which is exactly what will be discussed in the following topic.
Comments can be included anywhere which means inline as well. The best practice is to write relevant comments as and how you proceed with your code.
Here are some key points that will help you while commenting your code:
Now that you know the importance of comments, let’s move ahead and see how to write Comments in Python.
Comments in Python start with a # character. However, alternatively at times, commenting is done using docstrings(strings enclosed within triple quotes), which are described further in this article.
#Comments in Python start like this print("Comments in Python start with a #")
Output: Comments in Python start with a #
As you can see in the above output, the print statement is executed whereas the comment statement is not present in the output.
If you have more than one comment line, all of them need to be prefixed by a #.
#Comments in Python #start with this character print("Comments in Python")
Output: Comments in Python
The above output shows that all lines prefixed with a # character are not returned in the output.
Moving forward let’s see how comments are interpreted and why they never appear in the output.
How does Python interpret comments?
When the interpreter encounters a # symbol anywhere, (except inside a string because a # within a string just means #), it omits everything that is present after it until the end of that line. The # tag actually tells the interpreter to stop reading anything present after it.
They can appear either in an individual line or inline with some other code.
#multiplying two variables (this line starts with a #, hence will be ignored till line ends) a=1 b=2 c=a*b print(c) # printing result (inline comment, whatever is present after # will be ignored)
Multi-line comments appear in more than one line. All the lines to be commented are to be prefixed by a #. If you don’t do so, you will encounter an error.
#adding 2 variables #pinting the result in a new variable a=2 b=3 c=a+b print(c)
You can also a very good shortcut method to comment multiple lines. All you need to do is hold the ctrl key and left click in every place wherever you want to include a # character and type a # just once. This will comment all the lines where you introduced your cursor.
If you want to remove # from multiple lines you can do the same thing and use the backspace key just once and all the selected # characters will be removed.
However, these multi-line comments look very unpleasant when you’re commenting documentation. The following topic will introduce you to a solution to this.
Docstrings are not actually comments, but, they are documentation strings. These docstrings are within triple quotes. They are not assigned to any variable and therefore, at times, serve the purpose of comments as well.
They are used particularly when you need to affiliate some documentation related to a class or a function, etc.
""" Using docstring as a comment. This code divides 2 numbers """ x=8 y=4 z=x/y print(z)
As you can see, the output does not contain the docstring, therefore, it has been omitted as it appears before the code has started.
But if you execute just a docstring without the code below, as shown above, the output will be the string itself.
""" Using docstring as a comment. This code divides 2 numbers """
Using docstring as a comment.
This code divides 2 numbers
In the above output, the docstring has been printed since it is not followed by any code.
Now, in case it would be present after the code was written, the docstring will still be printed after the result.
x=8 y=4 z=x/y print(z) """ Using docstring as a comment. This code divides 2 numbers """
2.0 ' Using docstring as a comment. This code divides 2 numbers '
As you can see, the docstring has been printed after the output. So, therefore, as mentioned above, docstring behaves differently at different places depending on where it appears in the code. This brings us to the end of this article. I hope you have enjoyed learning about Comments in Python. Make sure you practice as much as possible and revert your experience.
Got a question for us? Please mention it in the comments section of this “Comments in Python” blog and we will get back to you as soon as possible.
To get in-depth knowledge on Python along with its various applications, you can enroll for live Python online training with 24/7 support and lifetime access.