When writing Python code, it is always a good practice to make your code clean and easily understandable. Organizing the code, giving variables and functions descriptive names are several ways to do this.

Another way to improve the readability of your code is to use comments. A comment is a human-readable explanation or annotation that is used to explain the code. For example, if you wrote a complex regex, you add a comment describing what the code does.

Adding comments to your Python code will save you a lot of time and effort when you look at your code in the future. Let’s say you want to change a script that you wrote a few months or years ago. The chances are that you will not remember why you wrote some complicated piece of code unless you added a comment. The comments also help other developers to understand your code and its purpose.

Comments should be short and to the point. Do not explain something that is obvious to the reader.

This article covers the basics of writing comments in Python.

Writing Comments in Python

Python ignores everything written on the line after the hash mark (#).

Comments can be added at the beginning on the line or inline with other code:

# This is a Python comment.
print("Hello World") # This is an inline Python comment.

Copy

The blank space after the hash mark is not mandatory, but it will improve the comment’s readability.

#python

How to Comment in Python
1.40 GEEK