Table of Contents Hide
Comments are a piece of text in a computer program that provides more information on the source code written. Like every other programming language, Python has three different types of comments: single-line comments, multiline comments, and documentation string to comment out block of code.
Introduction to Python Comment Block
Comments are used to explain the source code. Comments are mainly used for the following purposes.
- Improve Code readability
- Testing the code
- Explaining the code or Metadata of the project
- Prevent execution of specific code blocks
For example, let’s say you have written complex business logic, formulas, algorithms, etc. Then we need to document it using the comments that explain what the code does, thus improving the readability of the code in Python.
Python interpreter ignores the comments while executing the code and only interprets the code.
Types of comments in Python
There are three kinds of comments we can use in Python.
- Single-line comments
- Multiline comments
- Documentation strings, aka docstrings
Let us look into details on how to use these comments in Python code with examples.
Single-line comments, also called block comments, start with a hash sign (#) followed by a single space and a text string.
The hash (#) works with only a single line of code and not on multi-line code.
Let’s take an example to demonstrate single-line comments in Python.
# This is a single line comment example
If you place the comment in the same line as a statement, you will have an inline comment.
Like single-line comments, inline comments also begin with a hash (#) sign and are followed by a space and the comment text.
Let’s take an example to demonstrate inline comments in Python.
print("Hello World") # This is a example of inline comment
/* This is a comment block
Multi-line code */
But in Python Multiline comments do not exist built-in like other programming languages.
Python may not have any built-in mechanism for commenting out multiple lines. However, there are different ways to achieve this in Python.
We can use multiple hashtags to write multiline comments in Python. Each line that has a hash sign(#) is considered as a single-line comment.
# This is how we can acheive
# Multi-line comments in Python
Documentation strings, also called docstrings, are the string literal denoted with triple quotes that occur as the first statement in a module, function, class, or method definition. Conventions for writing good documentation strings (a.k.a. “docstrings”) are immortalized in PEP 257.
Note: We can also use triple
""" quotations to create docstrings.
Single line docstrings
Let’s take an example to demonstrate single line docstring.
'''Takes two number as input and returns sum of 2 numbers'''
Inside the triple quotation marks is the docstring of the function Add() as it appears right after its definition.
The multi-line docstring can span across multiple lines of code starts with triple quotes(
""") and ends with triple quotes (
We can use multiline docstring as multiline comments in Python to comment out block of code. The creator of Python, Guido Van Rossum, also recommended this and mentioned it on Twitter as a Pro-tip.
The following example shows you how to use multi-line docstrings. Make sure to indent the leading
''' appropriately to avoid an
'''Takes two number as input
Adds a and b
Returns sum of a and b as output
Note: As long as the string is not assigned to any Python variable, Python will read the code but then ignore it, and you have made a multiline comment.