Comments in Python

Before we dive into the actual topic Comments in Python, let’s understand why we need to add comments in our code.

Purpose of adding Comments in Python

In a programming language, we can define comments as an explanation about the source code of a program. In general, comments are added for the purpose of making the source code easier to understand by programmers. The comments should explain the programmer’s intent. It should explain the logic behind the code, rather than the code. Comments are generally ignored by compilers and interpreters

Its always a good programming practice to use comments. Because in the future if someone wants to modify the code, it’s easier for them to understand the source code with the help of comments.

Uses of adding Comments in Python

Debugging

Sometimes, you find it difficult to debug a large piece of code. In those cases, you can comment out a block of code, which will be ignored during the program execution. So that, its easier to find the source of error.

Metadata

Comments in Python are often used to store metadata of a program file. The metadata contains information like

  • the name of the person who created the program file.
  • date when it was created.
  • names of other people who have edited the code so far.

Syntax of Python comments

The syntax for the comments varies according to the programming language. In C++, we use // for single-line comments and /*…/* for multi-line comments. Similarly in Python, we have different syntax for single-line and multi-line comments in Python.

Single-line comment in Python

In Python, we use # to create a single-line comment. Whatever we add after the # will be ignored by the interpreter.

# assigning string to a variable
var='Hello world'

#printing a variable
print(var)

Multi-line comment in Python

Using #

We can use # at the beginning of every line for a multi-line comment.

#It is an
#example of
#multi-line
#comment

Using string literals

We can also use string literals as a comment. The string literals that are not assigned to any variable will be ignored by the interpreter.

'this is a comment'
print('Hello World!')
Hello World!

When we run the program, the interpreter ignores the string literal and executes only the print statement. We can use single quotes, double quotes, or triple quotes for a string literal.

In general, we use triple quotes for a multi-line string literal. Hence we can use triple quotes for a multi-line comment as well.

"""
Example of
multi-line
comment
"""
print('Hello World!')

Docstrings

Docstrings can be defined as a documentation Python Strings. The triple quotes that appear after the definition of a function, class, or method are docstrings.

def func(a):
    """Accepts a variable and prints it"""
    print(a)

func("Hello World")

We can check the docstring of a function, class, or method using the __doc__ attribute.

def func(a):
    """Accepts a variable and prints it"""
    print(a)

print(func.__doc__)
Accepts a variable and prints it
Translate ยป