Comments in Python

This tutorial will teach you how to add comments to your code. You’ll also learn about different types of Python comments, such as block comments, inline comments, and documentation strings.

You may want to document the code you write on occasion. You might wish to take note of why a piece of code works, for example. You use the comments to accomplish this.

Formulas, algorithms, and sophisticated business logic are typically explained with comments.

The Python interpreter ignores comments and only interprets the code when running a programme.

Block comments, inline comments, and documentation strings are the three types of comments available in Python.

Block comments in Python

A block comment clarifies the code that comes after it. A block comment is typically indented at the same level as the code block.

A block comment is made up of a single hash sign (#), a single space, and a text string. As an example:

# increase price by 5%
price = price * 1.05

Inline comments in Python

An inline comment is one that is written on the same line as a statement.

Inline comments, like block comments, start with a single hash sign (#) and are followed by a space and a text string.

The following is an example of an inline comment:

salary = salary * 1.02   # increase salary by 2%

docstrings in Python

A documentation string is a string literal used as the first line of a code block, such as a function.

A documentation string, unlike a typical remark, can be read at runtime using the obj. doc__ property, where obj is the name of the function.

To generate code documentation automatically, you usually use a documentation string.

Docstrings are documentation strings.

Docstrings are not, strictly speaking, comments. They refer to the strings with anonymous variables. The Python interpreter does not ignore them either.

There are two types of docstrings in Python: one-line and multi-line docstrings.

Single line docstrings in Python

A one-line docstring is exactly that: it only fits one line. A one-line docstring starts with triple quotes (“”) and finishes with triple quotes (“”) as well (“””). There will also be no blank lines before or after the single-line docstring.

In the quicksort() function, the following example shows a one-line docstring:

def quicksort():
""" sort the list using quicksort algorithm """
...

Multi-line docstrings in Python

A multi-line docstring, unlike a single-line docstring, can span many lines. A multi-line docstring begins with triple quotes (“”) and concludes with triple quotes (“”) as well (“””).

The example below demonstrates how to use multi-line docstrings:

def increase(salary, percentage, rating):
    """ increase salary base on rating and percentage
    rating 1 - 2 no increase
    rating 3 - 4 increase 5%
    rating 4 - 6 increase 10%
    """

Multiline comments in Python

Multiline comments are not supported in Python.

Multi-line docstrings, on the other hand, can be used as multiline comments. Python’s founder, Guido van Rossum, also endorsed it.

Maintaining a clear, brief, and explanatory comment is an excellent practise. The ultimate goal is to save you and other developers time and energy when working on the code.

%d