Python Tutorial

What are Python Comments? Explained in Simple Terms

What are Comments in Python?

A comment is added to a Python code using the # symbol. Its role is to provide further information. Comments also aid in the user's comprehension of the code.

Any programming language's comments are ignored and are not compiled or understood. Assumptions are added via comments. They explain how the application works for the user. This saves the user time. 

Consider the following example, in which each line of code has a remark to describe how the program works:

a = 1; b= 3 #Declaring two integers

c = a + b #adding two integers

print(c) #displaying the result

OUTPUT

4

Multi-Line comments

The hash character (#) at the beginning of each line can be used to add multi-line comments to several lines. 

Consider the following example:

#This program calculates

#sum of two integers

#and displays the result

Using the triple quotes ” ” ” is another technique to create a multi-line comment. These triple quotes are commonly used to denote multi-line strings, but they can also be used to denote multi-line comments. 

Consider the following example:

”””Inserting

    Multi line

    Comments to

    our program using

    triple quotes”””

Docstring in Python

The documentation string is abbreviated as the docstring. Triple quotes are used to produce docstrings in Python. A docstring is known as a string that appears as the first statement in a module, function, class, or method specification and describes the class, module, function, or method's functionality. 

Consider the following example:

Code:

 def function1():

    #Outer function of nested function 

       x = 1

       return x

def function2():

     #Inner function of nested function

       x = 2

print(function1())

print(function2())

Output:

1

None

Test your knowledge with a quick quiz!

In Python, which of the below options is used to define a code block?

Select the correct answer

Did you find this article helpful?