×

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.

  1. Single-line comments
  2. Multiple-line comments
  3. 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:

How to comment out a block of code in Python

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:

How to comment out a block of code in Python

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:

How to comment out a block of code in Python

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:

How to comment out a block of code in Python

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:

How to comment out a block of code in Python

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.


Related Topics

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

4 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Python enumerate() function

Python enumerate() function The enumerate() function in Python takes a collection (e.g. a tuple) and returns it as an enumerate object. Syntax: enumerate(iterable, start=0) Parameter Iterable:  This parameter represents an iterable object start:    This parameter represents a number defining...

1 minute read.