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.
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
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:
# This is your 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"
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
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.
This is your
to help you in coding
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.
"""Accepts a number cu, gives the cube of cu"""
"”” 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
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:
- By using the “#” symbol at the beginning of each line of the comment, or
- By using string literals in single (‘), double (“), or triple (“””) quotation marks.
# 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:
- Single-line comments
# List of numbers
numbers = [76, 5, 3, 68, 43, 23, 54, 42, 11]
- Multi-line comments
# Python code is
# easy to understand
# with comments
- String literals as comments
""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.
- Python docstrings
This function greets
the person passed as
print("Hello, " + name + ". How are you?")
Hello, Lisa. How are you?
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.
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.