Showing posts with label Python: comments. Show all posts
Showing posts with label Python: comments. Show all posts

Python: Comments

 We may describe the codes we develop, by writting comments in our program. In this article, we will study how to write comments in the program and also discuss about different types of Python comments.

Following are the advantages of using comments:

  • Readability of the codes.
  • Assists us in recalling why the specific codes were created.
  • Provide an overview of the program.
  • It may adds resources to the codes.
Types of Python comments:
In Python there are three types of comments, we can write along with the codes. These are single line comment, multi line comment and python docstring.
Single-Line Comment: A single line comment in Python has a hashtag (#) at the beginning of it and continues until the finish of the line. If the comment continues to the next line, add a hashtag to the subsequent line and resume the conversation.
Look at the following example which shows how to use a single line comment.
#This code is to show how to write a single line comment.
print('This is the statement without hashtag')

Output:
This is the statement without hashtag


The line starts with hashtag(#) is ignored by Python interpreter.
So we can use it only to explain our codes.
We can also write the above codes in the following manner:
print('This is the statement without hashtag')#This is example of a single line comment.

Output:
This is the statement without hashtag

Here, also the line starts with #, is ignored or omitted during execution of program.

Multi-Line Comments : However Python doesn't provide the facility of multi line comments, yet there are many ways to create multi line comments.
We may use hashtag(#) at the beginning of each line.
For example:
#This code is to show
#how to write 
#multi line comment.
print('This is the statement without hashtag')


Using String Literals: Because Python overlooks the string expressions, that are not allocated to a variable, so we can utilize them as comments.
For example:
' In this way, we can also insert a comment'


When we run the above code, we'll observe that there will be no output thus it can be used as comment. For multi line comment, we can use string inside triple quotes(""") instead single or double quotes.

"""This code is to show
       how to write 
       multi line comment."""
print('This is the statement without hashtag')

Python Docstring: The string enclosed in triple quotes that come immediately after the defined function are called Python Docstring. It is designed to link documentation developed for Python modules, methods, classes and functions together. It's placed just beneath the function, module or class to explain what they perform. The docstring is then accessible in Python using the_doc_attribute.
#Code to show how docstring works in Python

def add(x, y):
    """This function adds the value of x and y"""
     return x+y

#Displaying the docstring of the add function
print(add_doc_)


Output:
This function adds the value of x and y

Python: Functions