Comments in Python: Why are They Important And How to Use Them

Commenting in Python is the inclusion of short descriptions along with the code to increase its readability. A developer uses them to write their thought process while writing the code. It explains the basic logic behind why a particular line of code was written. They are only intended for the coders themselves or other developers to understand a piece of code, especially since the Python interpreter completely ignores comments in Python. You can see this in the example below.


What are comments used for

in Python?

Comments in Python are identified with a hash symbol, #, and extend to the end of the line. However, hash characters in a string are not considered comments. There are three ways to write a comment: as a separate line, next to the corresponding code statement, or as a multiline comment block.

There are multiple uses of writing comments in Python. Some significant uses include:

  • Increase readability
  • Explain the code

  • to others
  • Understand the code easily after a long term
  • Inclusion
  • of

  • resources Reuse of existing code

What are the advantages of using comments in Python? Commenting in Python

provides numerous advantages. Its main benefits include:

  • Makes code easily understandable to other programmers
  • Code is self-explanatory
  • Helps remember why we use a specific command, method, or function
  • in code

  • Allows the interpreter to ignore some part of the code while

testing What are the different types of comments in Python?

There are three types of comments: single-line, multi-line, and docstring comments. The syntax of comments varies by type. This tutorial will explore each type of comment individually, along with examples.

Single-line comments

Single-line comments begin with the “#” character. Anything that is written on a single line after ‘#’ is considered a comment. The syntax for writing

single-line comments is:

#comments here

There are two ways to use single-line comments in Python. You can use it before the code or next to the code. The example below shows the use of comments both ways.


PEP8, Python Style Guide, recommends using fewer than 79 characters in a single-line comment for readability. If your comment exceeds the recommended length, you can use the following type: multiline comments.



Python does not support multiline comments. However, there are several ways to overcome this problem. None of these forms are technically multi-line comments, but you can use them as one. The first way is by using # at the beginning of each line of the comment.


The next way is to use string literals but without assigning them to any variable. If you don’t assign a string literal to a variable, the Python interpreter ignores it. Use this to your advantage to write multi-line comments. You can use a single citation (”) or a double citation (“”).


You can also use multiline strings to comment. To do this, use the quotation marks ” or “” three times.


Python Docstrings Python provides a built-in feature called


for commenting modules, methods, functions, objects, and classes. They are written on the first line after defining a module, function, method, etc., using three quotation marks (” or “”). If you don’t use it on the first line, the interpreter won’t take it as a docstring. You can also access docstrings using the __doc__ attribute.


How to write good comments in Python?

Comments are a crucial part of a program. Therefore, it is essential to learn how to write good reviews. Here are some characteristics that define good reviews.

Make sure they are concise Don’t write generic comments

  • ; only have them if they add information

(a = 10

#assigning 10 to a, avoid writing such generic comments)

  • Write comments that describe the general task of a function or method and not specific details
  • Good

  • reviews are self-explanatory Don’t
  • write redundant comments


With that, we have reached the end of the ‘Comments in Python’ tutorial. Writing good comments in Python will allow other programmers to read and understand your code easily. It is one of the many basics in Python that you must learn to understand the programming language. With our Python tutorial for playlists for beginners, you can easily learn all about commenting and other concepts in Python.

Do you have any questions for us? Leave them in the comments section, and our industry experts will contact you about the same, as soon as possible!

Contact US