Python Programming (16 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (16 Blogs)
  • Data Science (16 Blogs)
  • Mastering Python (16 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

What are Comments in Python and how to use them?

225 Views
2 / 11 Blog from Python Fundamentals

Become a Certified Professional

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.

Before moving ahead, let’s just have a quick walk through all the topics that have been covered in this article –

What are Comments?

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. 

How to make use of Comments?

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:

  • Comments need to be short and relevant
  • They are to be specific to the block of code they are included with
  • Make sure to use decent language, as using foul language is unethical
  • Don’t comment self-explanatory lines

Now that you know the importance of comments, let’s move ahead and see how to write Comments in Python.

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.

Example:

#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 #.

Example:

#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.

Types of Comments

Comments can either be

  • Single-line or
  • Multi-line

Single-line Comments:

They can appear either in an individual line or inline with some other code.

Example:
#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)
Output: 2

Multi-line Comments:

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.

Example:

#adding 2 variables
#pinting the result in a new variable
a=2
b=3
c=a+b
print(c)

Output: 5

The above output shows that the first two program lines being prefixed with a # character have been omitted and the rest of the program is executed returning its respective output.

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.

Docstring Comments:

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.

Example:

"""
Using docstring as a comment.
This code divides 2 numbers
"""
x=8
y=4
z=x/y
print(z)

Output:  2.0

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.

Example:

"""
Using docstring as a comment.
This code divides 2 numbers
"""

Output: ‘\nUsing docstring as a comment.\nThis code divides 2 numbers\n’

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.

Example:

x=8
y=4
z=x/y
print(z)
"""
Using docstring as a comment.
This code divides 2 numbers
"""

 

Output: 

2.0
'\nUsing docstring as a comment.\nThis code divides 2 numbers\n'

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.  

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.