Write Single And Multi Line Comments In Python

There are two ways to write comments (docstring) in Python...

  1. # Single line comments - prpend any line with hash to write a comment
  2. """ Multiple line comments - add three tipple quotes to start and """ three tripple quotes to end the comment

Single Line Comments

To write single line comments in Python use literal '#'.

In [1]:
#Multiply two numbers
4*5
Out[1]:
20

There is no space required between # and your comment but for clarity it is good to have space after #.

In [2]:
# Multiply two numbers
4*5
Out[2]:
20

Indentation In Python Comments

Comments don't require indentation in Python

In [3]:
def mult(a,b):
# Multiply two numbers
    return(a*b)
In [4]:
mult(4,6)
Out[4]:
24

Mult-Line Comments In Python

One way to comment out multiple lines in Python is to add '#' in front of every line as shown below.

In [5]:
#Example below function takes two arguments and 
#Returns the result
def mult(a,b):
    return(a*b)
print(mult(5,4))
20

More preferred is to use tripple quotes before and after the comment as shown below.

In [6]:
"""
Example below function takes two arguments and 
Returns the result
"""
def mult(a,b):
    return(a*b)
print(mult(5,4))
20

Note - we can also use above method for single line comments.

In [7]:
""" Example below function takes two arguments and  Returns the result """
def mult(a,b):
    return(a*b)
print(mult(5,4))
20