Python Comments

by | Sep 13, 2021 | Python Programming

In this tutorial you will learn about documenting and commenting on the python code using python comments and python docstring.

Python Comments

Python Comments

It is tough to remember every variable’s name, function name, and class name when we have a hundred-page program or so. Therefore, making use of comments will make it very easy for you or someone else to read and modify the code.

Type of Python Comments

There are two types of comments Single-line comments and multi-line comments, which are available in python, or we can say which are available in every programming language.

Single Line Comment

A single-line comment is used by adding a hash sing (#) at the beginning of a text or a string statement.

# This is a single-line comment

Example Code:

print("This is A String ")
# This is Comment

Output:

This is A String

As we can see only, the print function is executed, and the comment was not printed on the output. We can add comments in the code, but this is not the only method to add single-line comments. We can also add comments after our function or end of the code.

Example Code:

#Heading

print("String start from here") #And Ends here

#Closeing comment

Output:

Staring start from here

Multi-line Comment in Python

We can create a python multi-line comment by adding a delimiter  (“”) on the starting and end of the statement.

"""
This is a multi-line comment.
Using python, and it will not be printed.
At the runtime
"""

Example Code:

print("Hello World")
"""
This is a multi-line comment
Using python, and it will not be printed.
"""

Output:

Hello World

Multiple Python Comments

We can add multiple comments in a single program or code.

# This is Header

"""
We are going to
Print hello world
"""
print("Hello World") # This is a print function

"""
Here we will print
Other print function
"""

print("It is Python comment") # Print function to print It is python comment

# This is Footler

Why Comments are Used in Python.

Comments in Python are used to explain code and what it does. They are meant as documentation for anyone reading the code.

Python Docstrings 

Python docstrings or Documentation strings are a string used in the class, module, function, or method definition. 

As like multiline comment, docstring is also declared using three (‘’’) or four (“””). For example  ‘’’ triple single quotes ‘’’ or “”” triple double quotes ””” Docstrings are accessible from the doc attribute (__doc__)   for any of the Python objects and built-in functions. Docstrings are great for understanding the functionality of the more extensive code of the project.

Example of Code:

def addition(n):
''' This is a docstrings example we have added in addition function '''
return n+n

print(addition.__doc__)

To run this docstring code, we have to follow this step.

print(addition.__doc__)

Here the output of string literal.

Output:

This is a docstrings example we have added in the addition function.

Here, we have documented our addition function, and then we are accessing it with the __doc__ attribute.

 We can learn more about python docstring from here.

Tips:

  • Remember to comment as often as possible.
  • Mainly used single-line comment after the code, so it will be easy to read code for others.
  • Comments don’t mess up with the code, so it is essential to add as many comments as possible for documentation.
  • The first includes comments that detail or indicate what a section of code – or snippet – does.
  • Think of the first type as a comment for yourself and the second as a comment for others.

 

Was this article helpful?
Share on: