Comments in Python

by | Jan 20, 2021 | Python

Home » Python » Comments in Python

Introduction

As developers, we are often told to maintain the comments throughout our code. But what are comments actually and why are they so important to use. In this article, we will understand the logic behind using comments.

Comments are the lines in our code that are ignored by compilers and interpreters. They are written with the purpose to provide the information about the logic used in source code to other programmers and serves as a note of reminder to ourself. Without comments, things can get challenging. It does not take part in the output of the programme but enhances the readability of code.

There are two types of comments used in Python:

  • Single line comment
  • Multi-line comment

Python Single Line Comment

We can infer the definition of the comment type from the name itself. A single line comment itself says that the comment in programming language which occupies one line space. It starts with a hashtag symbol (#) and lasts till the end of the line. And if the comment exceeds the line, then continue the comment in next line preceding with hashtag (#).

Generally, single-line comments are used for the short explanation of the expressions or variables.

Example:

# This is an example of single-line comment.

print(“Hello India!”)

 

Python Multi-line Comment

By the name multi-line comment we understood that it is about the comments written in more than one line. The comment text is enclosed within the delimiters ( “”” …….. “”””) or hashtags (#) are used at the start of each line. It is recommended to use # single comments instead of triple-quote string (“”””) as the comments may turn into accidental docstrings.

Multi-line comments are used for detailed description or explanation about the programme or code for the ease to read.

Example:

“”” This is an example of multi-line comment.

       We are going to print Hello India in our programme. Here, we will learn

       about the print statement.

       ….

“””       

print(“Hello India!”)

Importance of Comment in Python

Suppose you are working on a project and ignored to put comments. Down the line, the project ends up in 25,000 lines of code, and then there comes new developers into your project to maintain it. The new developers will take a hard time to understand and analyse your code, and by that time you may also take some time to understand the logic and hence it will delay the time to start working.

Using comments in your code helps you in this scenario. Comments helps the developers to understand the code quickly and enhances the code readability.

Syntax of a good Comment/ developer friendly comment

Below are some rules to follow while writing the comments in your code.

  • Keep short and simple comments: The lesser, the better.

Simple and short relevant codes are easy to understand and enhances code readability.

  • Avoid W.E.T comments:

Your code should be D.R.Y i.e. “don’t repeat yourself”. Writing comments for the piece of code which is self-explanatory is the simple mistakes in codes. This makes the code W.E.T i.e. “wrote everything twice”. There must be little to no redundancy in code.

Example,

print(“This is an example of comment!”)       #Prints This is an example of comment!

  • Include revision history:

In long-run, it can be useful to maintain the revision history in the programme.  It can be useful for the future developers while maintaining your code. When we look back to any code it gets important to know who made the changes and why it is made.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author