Methods and Functions

Python Comments (With Examples)

Comments in python are descriptions that makes the code easy to read. Python coding is easy when compared to other programming languages, but programmers still get confused when it comes to long blocks of code. Therefore, when coding in Python, it is crucial to make sure your code is understandable, not only to you but also to others.

comments in python

                                                                                                                                                       Source: nsouly.com

You already make your code simple by giving variables simple names, defining clear-cut functions, and organizing your code in the correct format. However, adding comments in your code helps you clearly describe what the given code will do. 

Advantages of Using Comments in python

Python comments

Python comments

Do you spot any difference in the two images given above?

 Yes! The first code block is difficult to understand because it does not have any comments. However, the second code block clearly explains what the class and functions do.

Comments are an essential part of a Python code and can be in the form of docstrings or in-line explanations that help us understand a line of code. Apart from making Python code easy to understand, comments also act as reminders or checkpoints in long code blocks. Apart from this, they are also used to ignore certain parts of a code while testing.

Single-Line Comments in python 3

 Inserting comments is quite easy. All you have to do is add the hash (#) symbol before the comment:

The great thing about comments is that Python compilers and interpreters do not read them during program execution. You can even add them in the same line as the code, like this:

                    

print("Hello World") # Printing "Hello World"

The Python interpreter ignores everything written after the “#” symbol till the line ends.

                    

# Printing "Hello World"
print("Hello World")  

 The output for both the above codes will be the same:

 

 Multiline comments in Python

We use multi-line comments or block comments when we want to explain complicated code or when we think a reader may not be familiar with the code. These long comments may apply to all or only some of the code that follows.

As Python does not offer a unique way to add multi-line comments, we use the “#” symbol at the beginning of each comment line

                    

# This is a multi-line or block 
# comment to help you
# understand

print ("We understand multi-line comments")

 String Literals for Multi-line Python Comments

 We can even write Python comments without any “#” symbol. How?

 The Python interpreter ignores any strings not assigned to a variable in the code.

                    

# This is a comment

 

'This also acts as a comment'

 In the above code, since the string literal is not assigned to any variable, it is ignored by the interpreter. To write a multi-line comment, we can use , , or “”” quotes.

Example:

                    

"""

This is your
multi-line comment
to help you in coding

"""

Python Docstrings

In Python, docstrings (documentation strings) are the set of string literals that we write after defining a function, method, class, or module. We write these using the triple quotes (“””…”‘””) and are used for documentation. However, unlike comments, we can access docstrings using the __doc__ attribute. 

 Example:

 

                    

def cube(c):

    """Accepts a number cu, gives the cube of cu"""

    return cu**3

print(cube.__doc__)

 Output:

                    

"”” Accepts a number cu, gives the cube of cu”””

 In the above code, we access the docstring of cube() function using the __doc__ attribute.

 How to Write Better Comments in python

Python comments

 You can improve the way you write comments by: 

  • Focusing on explaining what a function does instead of giving details of how the function does it.
  • Making the comments direct and straightforward.
  • Not adding unnecessary comments when possible.
  • Writing code that is self-explanatory.

 FAQs on Comments in Python

 Q1. How do you comment out multiple lines in Python?

 Answer: In Python, you can add multiple lines as comments in two ways:

  1. By using the “#” symbol at the beginning of each line of the comment, or
  2. By using string literals in single (‘), double (“), or triple (“””) quotation marks.

Example:

                    

# This is a good example
# of how you can arrange comments
# over multiple lines in Python

                    

"
    This is a good example
    of how you can arrange comments
    over multiple lines in Python

"


 Both the above examples show comments in multiple lines.

 Q2. What are the different types of comments in Python?

 Answer: Python comments are mainly of four types:

  1. Single-line comments

Example: 

                    

# List of numbers

numbers = [76, 5, 3, 68, 43, 23, 54, 42, 11]

  1. Multi-line comments

Example:

                    

# Python code is
# easy to understand 
# with comments

  1. String literals as comments

Example:

                    

""I can write a comment for list of numbers like this""
numbers = [76, 5, 3, 68, 43, 23, 54, 42, 11]

 Here, the sentence in the quotes is a string literal that acts as a comment. This is because it is not assigned to a variable.

  1. Python docstrings

Example:

                    

def hello(name):

    """

    This function greets
    the person passed as
    a parameter
    """

    print("Hello, " + name + ". How are you?")

hello('Lisa')

 Output:

In the above code, the lines written between the triple quotes are a docstring, and they explain the hello() function.

 Q3. How do you comment out a paragraph in Python?

Although we can use “#” to comment large paragraphs into a different line, it is easier to use the triple quotes (“””…”””) to write paragraph comments.

Example:

                    

"""

We use the def keyword to define a function in Python and the lambda keyword to define an anonymous function. The below code multiplies the argument 'a' four times with itself.

"""

four = lambda a : a * a * a * a

Q4. What do you mean by comments in Python?

 Answer: Python comments are simple sentences that we use to make the code easier to understand. They explain your way of thinking and describe every step that you take to solve a coding problem. These sentences are not read by the Python interpreter when it executes the code.

 The easiest way to add a comment to your code is to include a “#” sign at the beginning of your sentence. You can also use string literals that are not assigned to any variable as comments in Python.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.