Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

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

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read