0% found this document useful (0 votes)
12 views3 pages

Comments

Uploaded by

supriya sundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Comments

Uploaded by

supriya sundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Comments

Introduction to Python Comments


We may wish to describe the code we develop. We might wish to take
notes of why a section of script functions, for instance. We leverage the
remarks to accomplish this. Formulas, procedures, and sophisticated
business logic are typically explained with comments. The Python
interpreter overlooks the remarks and solely interprets the script when
running a program. Single-line comments, multi-line comments, and
documentation strings are the 3 types of comments in Python.

Advantages of Using Comments


Our code is more comprehensible when we use comments in it. It assists
us in recalling why specific sections of code were created by making the
program more understandable.

Aside from that, we can leverage comments to overlook specific code


while evaluating other code sections. This simple technique stops some
lines from running or creates a fast pseudo-code for the program.

Below are some of the most common uses for comments:

o Readability of the Code


o Restrict code execution
o Provide an overview of the program or project metadata
o To add resources to the code

Types of Comments in Python


In Python, there are 3 types of comments. They are described below:

Single-Line Comments
Single-line remarks in Python have shown to be effective for providing
quick descriptions for parameters, function definitions, and expressions. A
single-line comment of Python is the one that 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. Consider the accompanying code snippet, which
shows how to use a single line comment:
Code

1. # This code is to show an example of a single-line comment


2. print( 'This statement does not have a hashtag before it' )

Output:

This statement does not have a hashtag before it

The following is the comment:

1. # This code is to show an example of a single-line comment

The Python compiler ignores this line.

Everything following the # is omitted. As a result, we may put the


program mentioned above in one line as follows:

Code

1. print( 'This is not a comment' ) # this code is to show an example of a single-


line comment

Output:

This is not a comment

This program's output will be identical to the example above. The


computer overlooks all content following #.

Multi-Line Comments
Python does not provide the facility for multi-line comments. However,
there are indeed many ways to create multi-line comments.

With Multiple Hashtags (#)

In Python, we may use hashtags (#) multiple times to construct multiple


lines of comments. Every line with a (#) before it will be regarded as a
single-line comment.

Code

1. # it is a
2. # comment
3. # extending to multiple lines
In this case, each line is considered a comment, and they are all omitted.

Using String Literals

Because Python overlooks string expressions that aren't allocated to a


variable, we can utilize them as comments.

Code

1. 'it is a comment extending to multiple lines'

We can observe that on running this code, there will be no output; thus,
we utilize the strings inside triple quotes(""") as multi-line comments.

Python Docstring
The strings enclosed in triple quotes that come immediately after the
defined function are called Python docstring. It's 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 readily accessible in
Python using the __doc__ attribute.

Code

1. # Code to show how we use docstrings in Python


2.
3. def add(x, y):
4. """This function adds the values of x and y"""
5. return x + y
6.
7. # Displaying the docstring of the add function
8. print( add.__doc__ )

Output:

This function adds the values of x and y

You might also like