Comments in Python

Comments in any Programming language are used to increase the readability of the code. Similarly, in Python, when the program starts getting complicated, one of the best ways to maintain the readability of the code is to use comments. It is considered good practice to include documentations and notes in your code since it makes the code way more readable and understandable to other programmers as well, which comes in handy when multiple programmers are simultaneously working on same project.
The code can only explain how it does not why it does, but comments can do that. With comments you can make documentations for various explanations in your code itself.
In this module we will delve deeper into the concept of comments in Python. Following is the list of topics that we will cover in this module, in case you want to jump to a specific one.

So without any further delay, let’s get started.

Writing Comments in Python

Writing comments in Python
Comments are nothing but tagged lines of codes which increase the readability of the code and make the code self-explanatory. There are different ways of creating comments depending on the type of comment you want to include in your code. Following are different kinds of comments that can be included in your Python Program:

  • Single line Comments
  • Doc-string Comments
  • Multi-line Comments

Let’s discuss each one of the above-mentioned comment types separately.

Single Line Comment

Single line comments in Python are marked with # character. These comments end at the end of the physical line, which means that all characters starting after the # character till the end of the line are part of the comment only.
Example:

test= 7 * 2
print (test)
#Single-line comment

Output:

14

 

Docstrings

Python has the documentation strings (or docstrings) feature which is usually the first statement included in functions and modules. Rather than being ignored by the Python Interpreter like regular comments, docstrings can actually be accessed at run-time using dot operator.
It gives programmers an easy way of adding quick notes with every Python module, function, class, and method. To use this feature, we use triple quotes in the beginning of the documentation string or comment and again the closing triple quotes in the end of the documentation comment. Docstrings can be one liner as well as multi-liners.
Example:

def SayFunction():
”’
Strings written using ”’_”’ after a function represents docstring of func
Python docstrings are not comments
”’
print(“just a docstring”)
print(“Let us see how to print the docstring value”)
print(theFunction.__doc__)

Multi-line Comments

Unlike some other programming languages that support multi-line comments such as C, Java, and more, there is no specific feature for multi-line comments in Python. But that does not mean it is totally impossible to make multi-line comments in Python. There are two ways you can include comments that can span multiple lines in your Python code.

  • Python Block comments: You can also use several single line comments for a whole block. This type of comment is usually created to explain the block of code that follows the block comment. Python Block comment is the only way of writing a real comment that can span multiple lines. It is supported and preferred by Python’s PEP 8 style guide since Block comments are ignored by python interpreter or parser. However, nothing is stopping programmers from using the second ‘non-real’ way of writing multi-line comments which is explained below.
  • Using Docstrings: Docstrings are largely used as multi-line comments by many programmers, since it is the closest thing to having a multi-line comment feature in python. While it is not wrong to use docstrings when we need to make multi-line comments, it is also important that we keep in mind that there is a significant difference between docstrings and comments. Comments are totally ignored by the python interpreter while docstrings when used inside the functions can be accessed at run time.
test1= 7 * 2
type(test1)
”’
line one
line two
line three
”’

Output:

14
line one
line two
line three

This brings us to the end of this module, the next module highlights Python data types. See you there!

Leave a Reply

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

Solve : *
26 + 24 =