Comments in Python is the inclusion of short descriptions along with the code to increase its readability. A developer uses them to write his or her thought process while writing the code. It explains the basic logic behind why a particular line of code was written. They are just meant 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 following example.
What Are Comments in Python Used For?
Comments in Python are identified with a hash symbol, #, and extend to the end of the line. Hash characters in a string are not considered comments, however. There are three ways to write a comment - as a separate line, beside the corresponding statement of code, or as a multi-line comment block.
There are multiple uses of writing comments in Python. Some significant uses include:
- Increasing readability
- Explaining the code to others
- Understanding the code easily after a long-term
- Including resources
- Re-using the existing code
What Are the Advantages of Using Comments in Python?
Comments in Python provide numerous advantages. Their primary benefits include:
- Makes the code easily understandable by other programmers
- The code becomes self-explanatory
- Helps remember why we used a specific command, method, or function in the code
- Enables 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 depending on the type. This tutorial will explore every kind of comment individually, along with examples.
Single-line comments begin with the “#” character. Anything that is written in a single line after ‘#’ is considered as a comment. The syntax for writing single-line comments is:
# comments here
There are two ways of using single-line comments in Python. You can use it before the code or next to the code. The example depicted below shows the use of comments in both ways.
PEP8, Python Style Guide, recommends using less than 79 characters in a single-line comment to make it easier to read. If your comment is exceeding the recommended length, you can use the next type: multi-line comments.
Python does not support multi-line comments. However, there are multiple ways to overcome this issue. None of these ways 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 by using string literals but not assigning them to any variables. If you do not assign a string literal to a variable, the Python interpreter ignores it. Use this to your advantage to write multi-line comments. You can either use a single (‘’) quotation or double (“”) quotation.
You can also use multi-line strings for commenting. To do this, use either a ‘’ or “” quotation marks three times.
Python provides an in-built feature called docstrings for commenting on modules, methods, functions, objects, and classes. They are written in the first line after defining a module, function, method, etc., using three quotation marks (‘’ or “”). If you do not use it in the first line, the interpreter will not 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. Hence, it is essential to learn how to write good comments. Here are some characteristics that define good comments.
- Ensure that 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 overall task of a function or method and not specific details
- Good comments are self-explanatory
- Don’t write redundant comments
With that, we have come to 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 basic concepts in Python that you must learn to grasp the programming language. With our Python tutorial for beginners’ playlist, you can easily learn everything about comments and other concepts in Python.
Have any questions for us? Leave them in the comments section, and our industry experts will get back to you on the same, as soon as possible!