Comments in Python

Comments in python is like a notes, that will helpful when we revisit the code, or some other coder will understand what you have written.

It is always a good habit to write the comments in your codes.

Two types of Comments in Python

  • Single line comments
  • Multi line comments or Doc string comments

Single line comments uses the Hash (#) symbol before the line to mark as comment in Python.

Example for single line comment in python:

#Following print function prints a statement Hello World!
print("Hello World!")

The first line in the above code is a comment. Following example is a concise representation of a single line comments in python.

print("Hello World!") # print function prints a statement Hello World!

Unfortunately, there is no multi-line comment representation in python like C and Java. We can use only single line comments for multi line comments too. Following code is an example of Multi line comments in python.

# Following print function prints a statement Hello World!
# No semicolon required in Python
# Indentation is very important in python
print("Hello World!")

However, there is an another way called triple quote ''' this is not a multi line comment''' used for documentation purpose called docstring.

Ex:

''' DocString goes here, 
it can be multi line'''