How To Write Comments In Python

3 min read

Python itself is a versatile, dynamic programming language which strives to provide readable syntax. But as programs start getting bigger and more complex, they get more difficult to read.

It can be really difficult to look at a particular block of code and figure out what exactly it is doing.

For this reason, a good programmer should always add some notes explaining in plain language about what the code is doing.

These short informative notes are called comments. Comments are lines that exist in computer programs but ignored by compilers and interpreters

Comments are used to tell you what something does in human readable language, and they also are used to disable parts of your program if you need to remove them temporarily.

Writing good comments can save you from a lot of stress in the future.

Comments in Python

Comments in Python start with a # and a single whitespace character. Everything after this till the end of the line gets ignored by the compilers. However, they will remain in the source code for programmers to read.

# this is a comment

Now Python interpreter will ignore the entire line and move to the next line.

Another good example can be,

print("this will run")#this won't


this will run

Comments were invented to help developers maintaining and updating their code.

Good comments together with well-chosen variable names are necessary for any program longer than a few lines,  otherwise, the program becomes difficult to understand for both the programmer and others.

It requires some practice to write instructive comments. Never repeat with words what the program statement already clearly express. Use instead comments to provide valuable information that is not obvious from the code.

Inline comments

Comments which are just beside the code on the same line, are called inline comments. Generally, they look like this,

[Some code] # Inline comment about the code

Comments should always be short and to the point, Pep 8 suggests that a good program shouldn't have an inline comment more than 72 characters.

As you shouldn't put more than 72 characters comment in a single line, it's a good practice to divide them into multiple lines.

Multiline Comments in Python

In contrast to other programming languages, Python doesn't have  any out of the box solution for multiple lines, you just have to begin each line with a hatch # :

# This is how you
# can sperate your sweet
# Comments across
# Multiple lines

Comments can also be used to disable your code temporarily, by merely adding a #  at the beginning of the line which you want to disable and the compiler will skip the line.

# print("this won't run)
print("this will run")


this will run

Bonus Tip

Almost every modern text editor comes with a shortcut to add comments in your code press ctrl + / on PC and cmd + /on Mac, you can disable multiple lines by selecting those lines with the cursor and pressing ctrl + / on PC and cmd + /

Using comments in your programs makes it easy for others and your future self to understand the code, So you should always add relevant comments in your code, to make it more robust.

Hope this article helped you. If you have any questing regarding it feel free to drop a comment below.


Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read