Python Multi-line Comments

By | July 27, 2020

In programming languages, comments are used to provide additional information about the source code. So if some other developer read your code or you get back to your code after a long time, the comments tell us what a specific block of code is supposed to do.

Generally, comments are used to provide alternative information about the code snippet, but they also help in debugging code. If we comment out a part of code in python, then the python interpreter will not execute that part of code.

Vamware

Python Multi-line Comment

To write a multi-line comment in Python, we can use triple double-inverted(“”” Comment “””) or single-inverted(”’ Comment ”’) quotes.

Example:

def add(a,b):
    '''
     This is a user defined
     function to add two numbers
    '''
   return a+b

a= 200
b= 300

print(add(a,b))

Output

500

Note: While writing the multi-line comments make sure that you do not write it at the right side of the assignment operator (=) because triple double-inverted and single-inverted quotations are also used for multi-line string.

Example:

s ='''This is not a comment
it is a string'''

print(s)

Output

This is not a comment
it is a string

Python Single Line comment:

To write single-line comments in python, we use the hash symbol(#). Single line comments

def add(a,b):
    #This is a user defined
    #function to add two numbers
    return a+b

a= 200
b= 300

print(add(a,b))

Python Multi-line comments quick summary:

  • To write a multi-line comment, we use triple single-inverted and double-inverted quotes.
  • It is suggested to use consecutive single-line comments for multi-line comment. Because double inverted triple quotation also used for multi-line strings.

Leave a Reply

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