Comments in Python | Types, Example

In this tutorial, we are going to discuss the importance of comments in Python programming. It is important to understand the significance of writing comments while coding.

Comments in Python are non-executable statements written within programs that describe a step, process, important information, or the features of a program.

They allow programmers to compose and express their thoughts related to the code independently.

Writing comments in any complex program makes your code more readable and easily understandable for other programmers or team members.

Therefore, it is a good programming practice to write comments related to code in a program. We can write comments anywhere in the program that exists only for the programmers and is ignored by the Python interpreter.

How to write Comments in Python to Improve Readability?

In this section, we will learn how to write comments for a better understanding of complex code in Python.

a) In order to write a comment in a program, you simply put a hash symbol ‘#’ before the comment. In other words, use the hash (#) symbol at the start of your statement to write any comment.

The symbol # tells the Python interpreter to ignore the line and proceed to execute the next statement. For example:

# Hello, I am your first comment.

b) Everything after the hash symbol (#) is ignored by the interpreter and considered as a comment. Consider the following example comment.

print('Hello Comments!!') # Do not worry, I am a comment and will not be displayed on the console.

c) We can write comments to provide the information about variables, methods, classes, or any statement. For example, here is a comment that explains the value used to initialize a variable “a”:

a = 10 # Variable declaration and initialization.

The comment in the above code tells the declaration and initialization of a variable with a value. We can also use comments to hide program code in Python.

d) You should write comments short and precise in a program. As per PEP 8, if you are writing comments too long, then try to spread them over multiple statements or lines.

e) Python allows us to write multi-line comments with triple quotation marks. But, it is necessary to note that technically, writing anything within triple quotation marks is a string. We will learn more about it in the further tutorial.

Therefore, multiple lines within triple quotation marks are strings, not assigned to a variable. For example:


f) If you put # between single or double quotation marks it will be a ordinary character. For example:

msg = 'Hello # Comments'
      Hello # Comments

Types of Comments in Python

Comments are the important part of any program that helps to improve the readability and understanding of the complex program. In Python, there are mainly three types of comments. They are as:

  • Single-line comment
  • Inline comment
  • Multiline comment

a) Single-line comment:

A single-line comment begins with a hash symbol (#) and ends at the end of the current line. When we write a single-line comment in a program, the Python interpreter ignores everything after # mark. Consider the following single-line comment.

# Python program to find the sum of three numbers.

Here, the first line is starting with #, therefore, the interpreter will consider an entire line as a comment. Let’s see another example of single-line comment.

# msg is a method which will accept a parameter and print the value of it.
def msg(parameter):
msg('Welcome you to Scientech Easy!')
      Welcome you to Scientech Easy!

b) Inline comment:

When we write the code and at the end of the line, we write a comment on that line, then this comment is called inline comment. An example of inline comment is as follows:

x = 100 # Here, x is an integer variable that stores the value 100. 

In this example, x = 100 is a statement. After this statement, the inline comment starts with the symbol # that tells x is an integer variable and storing the value 100.

Note that indentation is quite important for Python suite. But, for comment, there is no certain rule that needs to follow the Python indentation. Consider the following example code based on it.

# city is a method which will accept a parameter and print the value of it.
def city(parameter):
# This statement will print the value of parameter variable.
    print(parameter) # indented by four spaces by default.

This is an example of writing comment without following Python indentation.

c) Multiline comment:

When we write a comment in multiple lines, then it is called multiple comment. Officially, there is no syntax to write a multiple line comments in Python.

Even so, we can write multiple comments using a pair of delimiters ” ” ” and ” ” ” (start and end with triple quotation marks) or with a pair ‘ ‘ ‘ and ‘ ‘ ‘ (start and end with single quotation marks).

Python interpreter ignores all the text written between these delimiters. In general, we use this style of comment on a part of line, an entire line, or to define a multi-line comment.

An example of a multi-line comment in Python is as:

""" Python
to calculate
the sum of
''' Python
to calculate
the sum of

Technically, Python does not support multi-line comment. Comments within triple double quotation marks or triple single quotation marks are actually regular strings.

This means that interpreter will allocate memory for these strings internally. There would be a waste of time for the interpreter having to check multi-line comment.

However, if we do not assign it to any variable, then the garbage collector removes it from the memory. Hence, we can reuse as comments. Let’s understand it with the help of a simple example.

to calculate
the sum of
def sum(x, y, z):
    s = x + y + z
    print("Sum of three numbers = ", s)
sum(10, 20, 30) 
      Sum of three numbers =  60

Recommend: Use a single line of comment instead of using multi-line comment.

Why should you write Comments in Program?

Writing comments in a program helps to improve the readability and understanding of the code. If you have written an application program, there is no doubt that you would know everything about it.

Now, suppose you have completed writing a thousand lines of code to develop an application. Suddenly, you take a break from your work for a month. After one month, you focus on some another project that needs immediate attention.

When you get back to the previous application program, the chances of you not remembering how the code worked are high.

Because of the absence of information or explanation about different sections will make it hard for you to read and understand your code. Starting from the point where you left the coding would not be easy.

In this situation, commenting will help you. It allows you to write a description of the logic right on top of your code in the script file.

So, when you will see a block of code after a long gap, you can easily read and understand what it does with the help of comments that you have left on the top of your code.

How do comments help others read your code?

Suppose you have a team of five coders working on a project. Suddenly, you feel that at least three more coders should join in to complete the project on time.

Now, if the code has no its comment, then you will have to put hard work to understand the code and also you will have to provide training to new members.

If new members do not get training, then it will be very difficult for them to get started with the development.

On the other hand, if you write comments on top of your code, you can make it easy for new coders to understand the logic of program.

Hence, you provide a small effort in writing comments on your behalf, your effort will save you from big trouble at a later time.

In this tutorial, we have elaborated on comments in Python with various examples. Hope that you will have understood how writing comments on the top of the code helps to improve the readability and understand the new coders.
Thanks for reading!!!
Next ⇒ Docstring in Python⇐ Prev Next 

Please share your love