×

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

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

How to Fix an EOF Error in Python?

Introduction An EOF (End-of-File) error occurs when a program tries to read beyond the end of a file or a stream, causing it to return an error message. It can happen...

8 minutes read.

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 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.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Python map() function

Python map() function The map() function in Python returns an iterator that applies a function to every item of iterable, yielding the results. Syntax map(function, iterable, ...) Parameter function: It is a required parameter that represents the function to...

1 minute read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.