Comments in Python

In general, writing comment means writing some text that defines your feeling or opinion or whatever you say about any particular thing. Similarly, comment in a Python program is used to provide description of a code, statement, or whole program.

Note - Comments in Python are human-readable or programmer-readable. That is, comment can't be read or interpreted by the compiler or interpreter.

What is a Comment in Python ?

So we can say, a comment is basically a human-readable explanation or annotation in the Python program. The explanation may be given of:

  • any particular statement
  • any block of code
  • any piece of code
  • sometime the whole program
  • or whatever the programmer need to explanation the thing in the program

Note - Comment can also be used to comment out any code or block of code to skip its execution by the compiler. Therefore, a comment can also be used for practice or debug.

Note - Generally, comments are used to provide in-program description of the code.

Why to Use Comment in Python ?

Most of the time, comment in Python is used to make source code more easier for other programmer to understand, or even the creator of the program too to understand, while reading the code now or later.

Another use of comment in a Python program is, if you found that a block of code in your program is not executing or performing its work in correct way. Then instead of deleting that block of code, you can comment it out, and create another block of code. May be while creating another new block of code, the error gets catch up, so that, just un-comment the previous block of code, and modify it. That's it.

Comment also helps in reviewing the Python code later. So, this article tells you all about the use of comment in Python programming along with example program.

Types of Comment in Python

Python programming language provides two types of comment to use in the source code of the program, that are:

  1. Single-line comment
  2. Multi-line comment

Single-line Comment in Python

Single-line comment in Python is a comment that is written after hash symbol (#). That is, all characters after hash (#) sign referred as comment (single line comment), up to the physical line end. For example:

# I'm a single-line comment of Python

print("Hey!\nHow are you?")

# I'm also a single-line comment of Python

This program produces exactly same output as shown in the snapshot given below:

python comments

As you can see from the above program and its output, only the statement:

print("Hey!\nHow are you?")

gets executed. Before telling anything further, let's create another program:

# I'm a single-line comment of Python

# print("Hey!\nHow are you?")

print("Comment Tutorial in Python Programming")

# I'm also a single-line comment of Python

Now this time, the program produces following output:

comments in python

You saw, how the statement:

print("Hey!\nHow are you?")

gets skipped for its execution by the compiler, only because of # (hash symbol) added before the statement. These are just some demo programs, shows how to comment the thing in a Python source code. Let's create another program, that uses comment to describe each and every step of the program:

print("Enter a Number: ")

# input() converts entered value into string, by default
a = input()

print("Enter another Number: ")
b = input()

# int() converts string to integer
a = int(a)
b = int(b)

sum = a+b

print("\nSum =", sum)

The snapshot given below shows the sample run of above program, with user input 12 and 34 as first and second number:

single line comment in Python

See how comment can be useful sometime to provide in-program description of all the codes. If you remove all the # (hash symbol) to un-comment the thing from above program. That is, if above program gets replaced with:

print("Enter a Number: ")

input() converts entered value into string, by default
a = input()

print("Enter another Number: ")
b = input()

int() converts string to integer
a = int(a)
b = int(b)

sum = a+b

print("\nSum =", sum)

Then the un-comment part also gets executed by the compiler. And you'll must see some syntax error message in this case, as shown in the snapshot given below:

comment example in Python

So it is proved that comments are ignored by compiler. Now let's talk about multi-line comment, then will summarize all the thing.

Multi-line Comment in Python

Multi-line comment is a comment that is written or expanded in more than one line or multiple lines. In Python, for multi-line comment, we use """ (triple quotes) or ''' (triple apostrophe). For example:

''' This is a
    multi-line comment
    in Python
'''

print("Hey\nHow're You?")

""" This is also a
    multi-line comment
    in Python
"""

If you run the above Python code, then you will see the following output on your output screen.

python single line multiline comment

The above program can also be written as:

''' This is a 
multi-line comment
in Python '''

print("Hey\nHow're You?")

""" This is also a
multi-line comment
in Python
"""

That is, the way of writing text inside the triple quote or apostrophe is up to you. Let's create another program that uses multi-line comment to provide in-program description of the source code:

print("Enter a Number: ")

''' The input() converts the entered value into string, by default.
    So when user enters 12 as input, it gets treated as string.
    That is, a = "12"
'''
a = input()

print("Enter another Number: ")
b = input()

''' Since a = "12", so adding both numbers say a and b
    gives "12" + "34". That is, "1234".
    If user enters 34 as second number.
'''
print("\nSum = ", a+b)

''' The int() converts string to integer.
    So int(a) or int("12") returns 12.
    Similarly, int(b) or int("34") gives 34.
'''
a = int(a)
b = int(b)

sum = a+b
print("\nSum =", sum)

""" Since first parameter of print() above is of 
    string type. Therefore second string must has 
    to be of string type to concatenate using +
    like shown below.
    The str() converts any type into string type
"""
print("\nSum = " + str(sum))

Here is its sample run with user input 12 and 34 as two numbers:

python multi-line comment

See, how easy the multi-line comments can be used to provide in-program description of the code. Because I've described the each and every code used in above program using the multi-line comment. Therefore no need to provide the description further. If any user including yourself see the program now or later. He/she including you must understand about all the code using given comment.

You can also use multi-line comment as single-line comment and single-line as multi-line comment. Let's see how.

Using Single-line Comment as Multi-line Comment in Python

To use single-line comment as multi-line comment in Python, we've to add # (hash) sign at each start of the comment line. For example:

# This is a single-line comment in Python
# The single-line comment is used here as multi-line

print("Hey!")

Disadvantage - The disadvantage of using single-line comment as multi-line, is to write hash (#) sign before each line of the comment.

Using Multi-line Comment as Single-line Comment in Python

The example given below shows how to use multi-line comment as single-line in Python:

''' This is a multi-line comment as single-line '''

print("Hey!")

Important Notice about Comment in Python

If comment are written just after the function, class, or method using multi-line comment (''' or """"). Then the comment can be accessed using the __doc__ attribute as shown in the program and its sample output given below:

def codescracker():
    """ Hey, This is a multi=line
        comment written in
        codescracker() function
    """

print(codescracker.__doc__)

Here is the output produced by above program:

multi-line comment in Python

Note - The way you write the code including spaces are all gets treated as inside the PRE tag of HTML. That is, everything including spaces, newlines, and of course the text itself gets accessed using __doc__ attribute.

Let's modify the above program, with the program given below:

def codescracker():
    """Hey, This is a multi=line
comment written in
codescracker() function
    """

print(codescracker.__doc__)

produces following output:

write comment in Python

Python Online Test


« Previous Tutorial Next Tutorial »