How to comment out a block of code in Python
When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business logic are normally explained using comments. Using the comments is how you do it.
The Python interpreter reads the code while running a program, ignoring the comments.
Block comments, inline comments, and documentation strings are the three types of comments offered by Python.
Uses of comment
- Observations are made. Any programming language you use requires you to make notes about your work in the code by adding comments.
- It clarifies the purpose of specific portions of the code and informs other programmers of your intentions when writing the code.
- Good programmers typically use the comment system to prevent the headache of trying to understand the code, which is a vital practice.
Python block comments
In a computer program, comments are a text section that provides additional details about the original source code. Python includes three different forms of comments, just like every other programming language: single-line comments, multi-line comments, and documentation strings to comment out code blocks.
The source code's comments serve as explanations. The following are the principal uses of comments.
- Make the code easier to read
- Code evaluation
- Describing the project's code or metadata
- Stop certain code blocks from running
Consider the case when you have created intricate business logic, calculations, algorithms, etc. Then, to make the Python code more readable, we must document it using comments describing what the code does.
While executing the code and just interpreting it, the Python interpreter ignores the comments.
Python's several comment types
In Python, there are three different categories of comments.
- Single-line comments
- Multiple-line comments
- The docstrings, or documentation strings
Let's examine in greater detail how to apply these comments to Python code using examples.
Single-line comments: Block and single-line comments begin with the hash symbol (#), one space, and a text string. The hash (#) only functions with single-line programs; multi-line programs are not supported.
Let us use an illustration to show how Python single-line comments work.
# displaying a string
Print('Hello world')
Output:

The comment in this case is:
displaying a string
The Python interpreter ignores this line.
Anything after # is not taken into consideration. The programme above can alternatively be written as follows on a single line:
Inline comments: An inline comment appears on the same line as a statement. Inline comments start with a hash (#) symbol, a space, and the comment content, much like single-line comments.
Let's use an illustration to show how inline comments work in Python.
print('Hello world') # This is an example of an inline comment
Output:

In this example, the comment is, This is an example of an inline comment.
As demonstrated below, we can typically make multi-line comments in languages like C, C#, Java, etc.
/*This comment block accepts multi-line code; thus, it begins with */.
However, unlike other programming languages, Python does not have built-in support for multi-line comments.
There may be no built-in method for commenting out numerous lines in Python. In Python, there are various approaches to accomplishing this.
Making Use of Several Hashtags (#)
Python allows us to construct multi-line comments by using several hashtags. Hash sign(#) lines are regarded as single-line comments.
# By doing this, we can get
# Multi-line comments in python
print('Hello world')
Output:

Each line in this case is viewed as a single comment, and each one is disregarded.
String Literals for Multi-line Comments
Although there isn't a specific technique to write multi-line comments in Python, we do know that the interpreter ignores string literals that aren't allocated to a variable.
We can therefore use the following as a single-line comment:
#it can be a comment
'so is this '
The second line of the programme is a string, as seen above, but it is not allocated to a variable or function. Thus, the string is ignored by the interpreter.
Similarly, multi-line comments can be written using multi-line strings (triple quotes).
It is possible to use either'or'as the quotation character.
Writing Multi-line Comments with String Literals
'''
This is a
Multi comment
'''
print("Hello World")
Output:

The interpreter disregards the multi-line string because it isn't set to a variable in this case. It can be used as a multi-line comment even though it isn't officially one.
The docstrings, or documentation strings
def square(k):
'''Takes in a number k, returns the square of the k'''
return k**2
print(square.__doc__)
Output:

The string literal used here is:
'''Takes a number k, returns square of k'''
The docstring for the function square() is contained within the triple quotation marks, following its definition.
Note: Triple """ quotes can also be used to produce docstrings.