How do you write a single line and multiple lined comment in python?

You are here: Home / Comments / Single Line and Multi Line Comments in Python

A comment is a piece of code that isn’t executed by the compiler or interpreter when the program is executed. Comments can only be read when we have access to the source code. Comments are used to explain the source code and to make the code more readable and understandable. In this article, we will see how to write single line and multi line comments using different methods in python.

What is a single line comment in python?

Single line comments are those comments which are written without giving a line break or newline in python. A python comment is written by initializing the text of comment with a # and terminates when the end of line is encountered. The following example shows a single line comment in a program where a function is defined to add a number and its square to a python dictionary as key value pair.


#This is a single line comment in python
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict

We can also add a single line comment after another statement.

#This is a single line comment in python
print["Pythonforbeginners.com"] #This is also a python comment

What is a multi line comment?

As the name specifies, a multi line comment expands up to multiple lines. But python does not have syntax for multi line comments. We can implement multi line comments in python using single line comments or triple quoted python strings.

How to implement multi line comments using # sign?

To implement multi line comments using # sign, we can simply depict each line of a multi line comment as a single line comment. Then we can start each line by using # symbol and we can implement multi line comments.

#This is a multiline comment in python
#and expands to more than one line
print["Pythonforbeginners.com"]

When writing multi line comments using # symbol, we can also start multi line comments after any python statement.

#This is a multiline comment in python
#and expands to more than one line
print["Pythonforbeginners.com"] #This is also a python comment
#and it also expands to more than one line.

How to implement multi line comments using triple quoted strings?

Multi line strings in python can be used as multi line comments if they are not assigned to variables. When the string isn’t assigned to any variable, they are parsed and evaluated by the interpreter but no byte code is generated because no address can be assigned to the strings. Effectively the unassigned multi line string works as a multi line comment.  

"""This is 
a 
multiline comment in python
which expands to many lines"""

Here we have to keep in mind that the multi line comments are only string constants that have not been assigned to any variable. So they have to be properly intended unlike single line comments with # symbol so that syntax errors can be avoided.

Also, multi line comments which use triple quotes should always start with a newline which is not the case for a single line comment. 


#This is a multiline comment in python
#and expands to more than one line
"""This is 
a 
multiline comment in python
which expands to many lines"""
print["Pythonforbeginners.com"] """This is not 
a 
multiline comment in python
and will cause syntax error"""

Conclusion

In this article, we have seen how to write single line and multi line comments in python. We have also seen how to write multi line comments using strings. Stay tuned for more informative articles.

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

We have learned about the Python shortcut operators in the previous tutorial that reduces the length of the expression and increases the readability. In this tutorial, we will discuss how to make our code readable and easy to understand for others by using Comments in Python. You are going to learn the following topics in this tutorial.

  • What are Comments in Code?
    • Why are comments beneficial during coding?
  • Comments in Python?
    • How to write Single line Comments in Python?
    • How to write Multiple line Comments in Python?

What are Comments in Code?

Every programming language has a concept called comments. What are these comments? And what's the use of these comments in the code?

Comments are the statements that are ignored by the interpreter or compiler at runtime.  If you have comments in the code, then the compiler ignores those statements. Moreover, every coding language has its syntax, which differentiates a comment from actual code.

Why are comments beneficial during coding?

  • Future Reference - Assume that you have written a program which contains several hundred lines of code without any comments. If you open the program after one or two months, then you will find it challenging to understand your code. Additionally, in most cases, we will forget the role of a code block, and again we have to trace it to understand. So putting a comment in code saves a lot of time when you have to update the code again.

  • Comments help to understand the code of others - We usually work in a team structure, where others review the code of one team member. Also, you might need to work on a feature that was earlier worked by some other developer. In both cases, you will need to understand the code that someone else has written. If you have used comments in your code, it makes the life of the other person looking at your code much easier. He can do a useful review, and also chances of defects are much lesser if someone else is editing your code.

  • Code Debugging - Sometimes, we have to check a part of the code to see whether it's working as expected or not. In this case, we can comment on the remaining code. Without comments, we will have to remove the code to check the output. If it's not working, then we have to enter the same code again. It's a pretty time-consuming process.

Now we know the comments, and it's importance, let's take a look at the syntax of the comments in Python.

How to write comments in Python?

In Python, any statement which begins with a hash [#] symbol is a comment. So, any code you write after the hash [#] symbol doesn't get executed.

Single-Line Comments in Python

We can write comments at any point in the program. See the examples.

# variable to store the name of the company
toolsqa = 'ToolsQA'

The first line in the code is a comment that explains the variable below. You won't get any output if you run the above code as we didn't print anything. Let's write a program to add two numbers with comments.

# initializing two variables
a = 5
b = 7

# adding a and b
result = a + b

# printing the result of the addition
print[result]

Even non-programmers able to understand the above code. The interpreter ignores all the statements that begin with a hash [#] symbol. Open the PyCharm and run the above program.

Commenting the code is good practice. Make it as a habit. We are going to practice more in the following tutorials. Maybe you feel commenting in the above program is an overdose. But for beginners, it's a good practice to comment even for the small statements. Once you are familiar with the programming, then use comments for a specific block of code at once. Let's see an example of code debugging.

# initializing variables
a = 5
b = 7

# result = a + b
d = b - a

# printing the value
print[d]

We made the result = a + b as a comment in the program. Here, we are checking the other piece of code. We may see the result in the next steps. So, we didn't remove it.

Multiple Line Comments in Python

What if you want to write a comment on multiple lines? Is it possible in Python?

Yeah!  We can put a hash [#] symbol in front of each line for multi-line commenting. Let's see an example.

# Read the input from the user
# Perform some operations on the input data
# Output the result
print["Multi-line commenting"]

Conclusively, that's it for the tutorial. Let's wrap it up here.

Key Takeaways

  1. Firstly, the Python interpreter will ignore the comments at the runtime of the program.
  2. Additionally, we will use hash [#] sign to begin a comment in Python.
  3. Moreover, use hash [#] in front of each line for multi-line comments.
  4. Comments help to recognize the functionality of a code block.
  5. Additionally, it's useful when debugging the code.
  6. Lastly, comments act as a reference for the future.

Practice Yourself


#Program 1


print["#Hello", end=' ']
#print["ToolsQA"]
print["#Python"]
#Program 2


# This program
adds two number #
print[2 + 5]
#Program 3


# a = 5
# a += 1
print[a]
#Program 4


num = 5
# num = 0
print[num]

What Next?

To conclude, we have learned how to comment on the code in Python. It's one of the essential parts of coding. So, make sure that you comment on your code. Additionally, it saves a lot of time in reference.

Till now, we have hardcoded the values to the variables and expressions. In the next tutorial, we will see how to take data from the user using the Python Input Function.

Happy Coding :]

How to use multi

Here, the first two lines contain hash character [#] and the interpreter prevents the two lines from execution. Then it prints the “Python Comments” and finally, it will prevent the last line from execution. Using Multi-line string as comment Python multi-line comment is a piece of text enclosed in a delimiter ["""] on each end of the comment.

How to use consecutive single

Consecutive single-line comments can be used as multiline comments in Python. Here, the first two lines contain hash character [#] and the interpreter prevents the two lines from execution. Then it prints the “Python Comments” and finally, it will prevent the last line from execution.

How do you write a comment in Python?

A python comment is written by initializing the text of comment with a # and terminates when the end of line is encountered. The following example shows a single line comment in a program where a function is defined to add a number and its square to a python dictionary as key value pair.

Can Python read multiple lines of code in a string?

As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment.

How do you give single line and multiline comment in Python?

Let's have a look at them!.
Using multiple single # line comments. You can use # in Python to comment a single line: # THIS IS A SINGLE LINE COMMENT. ... .
Using triple-quoted string literals. Another way to add multiline comments is to use triple-quoted, multi-line strings..

How do you write single line comments in Python?

In Python, we use the hash symbol # to write a single-line comment.

What is single line and multiple line comment?

Single-line comments begin with a double hyphen [ - - ] anywhere on a line and extend to the end of the line. Multi-line comments begin with a slash-asterisk [ /* ], end with an asterisk-slash [ */ ], and can span multiple lines.

How we can add single and multi line comment?

You can create a single line comment by putting -- at the start and --> at the end of your comment. You can also make a multi-line comment in HTML by adding at the end of your multi-line comment.

Chủ Đề