In this post, we are going to understand different ways of How to do comments in Python with code examples. In Python, we can add comments in the code in three ways. These ways are block comments, inline comments, multiline comments, and Docstrings. In this post, we will cover all these.
Explanation of Comments in Python
The code should be easily understood by other developers.The better way to make our code more readable is by using comments. The comments are explanation of code that makes it easy understandable to the programmer and easily figure out why the same block of code have been written.The Python interpreter ignores the comments.
Sometime , Even the comments are used to ignore some code statements.
1.Block comments
The code block comments in Python are used to describe more about the code to understand it better. It describes the code that follows its indent at the same level as the code block.
In Python block comment each line starts with hash(#) single space followed by the text string.
Program Example
The block comments in the below example is describing what task this program is performing.
#define a sum method
def sum(a,b):
#adding two number to calculate the sum
sum = a+b
print('sum of two integer :',sum)
#calling the sum() function
sum(12,24)
2. Inline comments
The inline comments are placed on the same line as the code statement. The line comment also starts with a hash(#) and a single space as block comments. These comments are mostly used to describe single-line code statements.
Programm Example
def sum(a,b):
sum = a+b # sum of two number
mutiply = sum*2 # mutiply sum of two number to 2
print('sum of two integer :',sum)
print('sum of two integer :',mutiply) #printing the mutiplication result
sum(12,24) #calling the sum() function
3. Multiline comments
Python does not support multiline comments we can use block comments. In the case of multiple text strings, each line is separated by a hash(#) and a single space. But in Python , we have support for multi-line strings (doc-strings) and those can be used as comments.We will cover them in other tutorial , but you can get some idea in this article with following topics.
Program Example
# sum() function to find sum of two number
#printing the sum of result result
# mutiply sum of two number to 2
#calling the sum() function to get result.
def sum(a,b):
sum = a+b
mutiply = sum*2
print('sum of two integer :',sum)
print('sum of two integer :',mutiply)
sum(12,24)
4. Python Docstrings
The docstring (document string) is a string literal. It appears immediately after function, class, module, or method definition. We can access docstring runtime using __doc__ attribute or help() method.
The Docstrings are used for better understanding of large code that includes classes, modules, functions.
Types of docstring
- One-line doctstrings
- Mutiline docstrings
One-Line Docstrings
The python docstring fits in one line. The one-line docstring starts with triple single quotes (”’)or triple-double quotes (“””) and also closes with same quotes.
The opening and closing quotes are in the same line in the case of the One-Line Docstring.
Programm Example :One-Line Docstrings
def SUM_function(a,b):
''' sum of two integer numbers'''
sum = a+b
print('sum of two integer :',sum)
print(' Docstring:',SUM_function.__doc__)
SUM_function(12,24)
The output will be the sum and comments using __doc__ attribute.
sum of two integer : 36
Docstring: sum of two integer numbers
Muti-line docstrings
The multiline docstring starts with triple single quotes (‘ ‘ ‘) or triple-double quotes (“””) and also close with the same quotes. In this example, we are writing multiline string using single triple quotes (‘ ‘ ‘).
Programm Example :One-Line Docstrings
def SUM_function(a,b):
''' sum of two integer numbers
printing the result using print() method
doctstring using the __doc__ attribute
'''
sum = a+b
print('sum of two integer :',sum)
print(' Docstring:',SUM_function.__doc__)
SUM_function(12,24)
The output will be sum and comments that we are accessing using __doc__ attribute.
sum of two integer : 36
Docstring: sum of two integer numbers
printing the result using print() method
doctstring using the __doc__ attribute
Summary :
We have explored different ways to understand about How to do comments in Python with code examples that include block comments, inline comments, multiline comments, Docstrings.