Comments in Python: Why Are They Important and How to Use Them

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.

commentsinPythonexp

commentsinPythonexp1

What Are Comments in Python Used For?

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

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

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

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.

Single-LineComments

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.

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.

Multi-LineComments

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.

Multi-LineComments2

You can also use multi-line strings for commenting. To do this, use either a ‘’ or “” quotation marks three times.

Multi-LineComments3.

Python Docstrings

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.

PythonDocstrings

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

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
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Conclusion

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!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.