Skip to content

Python Comments

Python comment

Python Comments is the part of the Python program that is ignored by the Python interpreter during program execution.

Why to use Comments in Python ?

When we write a code, we can easily understand that code. But when we have to come back to a code after a long time to check it, in which logic we wrote the code , then it takes us a long time to understand. 

So when we write a code, we write a comment in a certain place of the code which explains a certain part of the code. So even after a long time, if we come back to that code, then by looking at the comment, we can easily understand why and how that part of the code has been written.

What can Python Comments do?

Here we will discuss what comments can do in Python –

  • Comments can be used to explain a program.
  • If we do not want to execute a specific line in a program, we can use that line as a comment.
  • Comments are used to make a program readable.

How to use Python Comments?

In Python we use the “#” symbol to write a comment. Anything written after “#” Python will treat it as a comment.

Single line Python comment

Single line comment is the comment that starts and ends in the same line.

Example 1:

# This is first comment 
print("Hello, World!") # This is second comment
Hello, World!

In the above code everything in the first line is after the “#” symbol, so Python has treated the whole line as a comment.

But in the second line, the “#” symbol is in the middle of the line. So Python treat the previous part of the “#” symbol as Python code and the next part as Python comment. Since the code part has a print function, Python will print the whole data inside the print function at the output.

Multi line Python comment

Multi line comment is the comment that starts in a line and ends in a different line. There is no such thing as Multi line comment in Python. Multi line comment in Python is the sum of several consecutive single line comments.

Example 1:

# This is 
# a multiline 
# comment

In the above code, each line is a single line Comment which has been combined to form a Multi Line Comment.

Using string as Multi line Python Comment

We can use Python’s string as a Multi line Comment.

Example 1:

''' This is 
a multiline 
comment '''

Example 2:

""" This is 
a multiline 
comment """

In the above code Python string is used as Multi line Comment. In the first case “Triple Single Quote” has been used and in the second case “Triple Double Quote” has been used.

Remember, Python never reads a comment, but when we use a string as a comment, Python reads that string. Since the string is not assigned to anything in this case, the string will have no effect on the output.

Note :

In VS Code or Jupyter Notebook we can comment or comment out any line of the program using “Ctrl + /”.

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply