Python Comments

Comments plays an important role in programming. In programming, a comment is simply a text to describe the working of a single instruction or a set of instructions. Though the comment is written within a program, its not actually a part of it. This means the comment would not be executed along the instructions written in the program when the program is executed.

In python, a comment starts with the hash (#) sign and ends with end of the line.

For example-

#This program will display Hello World
print("Hello World")

The text “#This program will display Hello World” in above program is a comment. It is written to tell a person what this  particular program will do.

Note:- You can write a comment anywhere in the program, beside an instruction, before it of after it. But its a good practice to write a comment before an instruction.

Multi line comments in python

Unlike other programming languages like C, C++ or Java, there is no way for writing multi line comments in python. If you want to write a multiline comment, you simply need to write multiple single line comments starting with hash.

#This program
#will display
#Hello World
print("Hello World")

However, You can also write a multiline comment by writing a multiline string. String literals that are not assigned to a variable are ignored by the python interpreter. You can place your multiline comments within triple quotes in your program.

'''This program will
display Hello World'''
print("Hello World")

Comments are not always written to describe the code, sometimes they can also be used to temporarily disable a part of our code.

For Example-

a = 4
b = 5
#print(a)
print(b)

In above program, we have commented the line that was intended to print the value contained by the variable ‘a’. As it has become a comment, this line would not be executed when the program runs.

Note:- As long as a string literal is not assigned to a variable, it is ignored by the python interpreter.