Python Comments

Comments are one of the useful things available in various programming languages. Comments enhance the readability of the code. The programmer can easily understand the code if it is well commented.

These are the lines in the code that the interpreter ignores during the program’s execution. The comment can be anything, a sentence to understand the code or a python code statement that we don’t want it to execute.

Why are Comments Useful?

Let’s consider two different scenarios to understand the importance of comments in the programming world.

Code without Comments

perc = 0
a = 20
b = 60

perc = ( a / b ) * 100
    
print(perc)

If you look into the above code snippet, it’s hard to know what the code is doing and what is the final output.

Code with Comments

#initializing a variable perc to 0
perc = 0

#initializing variable a and b
a = 20
b = 60

#calculating percentage of a in relation to b
perc = ( a / b ) * 100

#displaying the final percentage
print(sum)

On the contrary, the comments make it straightforward to understand and read the above code snippet.

So, writing comments while developing is a great practice to avoid the above situations.

Comment Types in Python

Comments can be used in Python in three different ways.

  • Single-line comments
  • Multi-line comments
  • Docstring comments

Single-Line Comments

To comment single-line in python simply add a hash(#) at the start of the line. Everything on the right of hash(#) will be commented. If you want multiple lines to be commented then you need to add a hash(#) at the start of each comment line.

#printing "Welcome to the CodeLucky.com"
print("Welcome to the CodeLucky.com")

#This is the first single-line comment
#This is the second consecutive single-line comment
print("Hello this is Python Comment Tutorial")

Single-line comments can also be used to prevent the execution of any python code statement. Comment can also be placed at end of the python code statement.

#print("Comment prevent this line to execute")
print("Hello Python")

multi = 0 #intializng mult variable to 0

multi = 10 * 20 #muliplying 10 * 20 and storing in multi

print(multi) #display multiply result of 10 * 20

Multi-Line Comments

Python doesn’t have a way to write multi-line comments like in C, JAVA. In C, JAVA you can include multiple lines between /* and */. 

/* Everything inside this will act as 
a multiline comment in C, JAVA. */

In Python to add a multi-line comment, we can insert hash(#) at the start of each line.

#This is a
#multiline comment
#to demonstrate 
#multi-line comments in Python
print("Multi-Line Comments")

Another way to do this is to use a string literal. Python ignores the string literal which is unassigned to any variable, so it can act as a multiline comment. We can either use a single triple quote (''') or double-triple quote(""").

'''This is another way to 
use multi-line comments
in python'''
print("Multi-Line Using Single triple quote")

"""Double triple quote can
also, act as a multi-line
comments in python"""
print("Multi-Line Using Double triple quote")

These triple quotes (''') and (""") are generally used for multi-line strings, but as long as string literals are not assigned to any python variable, we can add our comment inside it. Python will read these lines but will ignore them.

Docstring Comments

Docstring stands for Documentation String.

Python docstring is the string literals with triple-double quote("""). They are added right below the definition of module, function, class, or method. Docstring is used to describe what the source code is doing.

def add(a, b):
    """add the given operands
    
    Parameters
    a : int
        first operand
    b : int
        second operands
    
    Returns
    c : int
        addition of two operands
    """
    c =  a + b
    
    return c

addition = add(12, 43)
print(addition)

We can then access the docstring with the __doc__ attribute of that object.

def add(a, b):
    """add the given operands
    
    Parameters
    a : int
        first operand
    b : int
        second operands
    
    Returns
    c : int
        addition of two operands
    """
    c =  a + b
    
    return c

print(add.__doc__)

Leave a Reply

Your email address will not be published. Required fields are marked *