Python Comments
Comments in Python are the simple lines in code that are ignored by the interpreter. Comments are not part of the code; they are simply used to explain the code.
When we write code, it is important that our code can be easily understandable by others. Python comments provide this feature so that we can explain our code to others. Comments also increase our code’s readability. For example:
Code:
num1 = 10
num2 = 10
#these num1 and num2 are the two variable.
print(num1 + num2)
#Here we using print function
Output:
20
Now we only get 20 in result; the text we wrote on line 3 and line 5 didn’t show in output because these lines are comments.

Advantages And Uses of Comments in Python
These are the following advantages and uses of comments:
1: It could enhance our code readability.
2: We could easily explain our code to others by using comments.
3: We could easily understand our code itself if we studied our code after sometimes.
4: We can easily share and collaborate our code with others if we use proper comments in our code.
5: We can also restrict some code for execution by making them comments.
6: If you use proper comments, it can provide an overview of your program or your project.
How to Write Comments in Python?
We already learned in the previous example that when we write comments, we use the hash (#) sign. It is also called a pound sign. So every line in Python that starts with a hash (#), the interpreter will ignore that line and treat that line as a comment. For example:
Code:
first_name = 'John'
last_name = 'Malkovich'
# John Malkovich is an actor.
print(first_name, last_name)
Output:
John Malkovich
We start the third line with a hash and write some text after the hash sign (# John Malkovich is an actor). Now this line is a comment, and when our program runs, the interpreter does not show this line in the output. So comments are not visible in output. And everything we write after the hash till the end of the line will be treated as comments.
Prevent Code From Execution
In the previous example, we used normal text as a comment. In our program, if we want to prevent some code from execution, we can make that code a comment. For example:
Code:
num1 = 10
num2 = 5
print(num1 + num2)
print(num1 - num2)
print(num1 * num2)
Output:
15
5
50
Now here we have three print functions that are doing arithmetical operations on variables. Now, for some reason, if we want to stop some arithmetical operations in output, instead of deleting them, we make them as comments. For example:
Code:
num1 = 10
num2 = 5
print(num1 + num2)
#print(num1 - num2)
#print(num1 * num2)
Output:
15
So this is how we can use a comment to prevent some code from execution.
Types of Comments
Basically, there are two types of comments in Python.
1: Single Line Comments
2: Multiline Comments
Single Line Comments
When we write our comment in a single line. It is called a single-line comment. For example:
Code:
num1 = 100
# num1 is a variable.
print(num1)
Now here this comment (# num1 is a variable) is a single line comment. However, it is not necessary to write a comment in a new line; we can write as an inline comment too. For example:
Code:
num1 = 100 # num1 is a variable
print(num1)
Now here we write comment in the same line as code, and this is called inline comment. Now remember, when we write an inline comment, we provide some space between code and comments.

However, it is only recommended to provide space between code and comment when you are writing an inline comment. It is not necessary to do that. If you didn’t provide any space, your code still works.
Code:
num1 = 100# num1 is a variable
print(num1)
Your code works fine, but this is not recommended.
Multiline Comment
There are two ways to write multiline comments. The first way is the same as single-line comments, but we use multiple single lines this time:
Code:
num1 = 10
#num1 is first variable
num2 = 25
#num2 is second variable
print(num1 + num2)
#adding both variable
Output:
35
Here we used multiple single-line comments; that’s make it multiline comments. We can mix inline comments or we can write all comments in one place. For example:
Code:
num1 = 10 #num1 is first variable
num2 = 25 #num2 is second variable
print(num1 + num2)
#using print function for output
#we are adding both variable
So this is the first way to write multiline comments, or we could say the official way of writing multiline comments. There is another way to write multiline comments.
How to Write Multiline Strings as Multiline Comments in Python?
Python doesn’t have any specific syntax for multiline comments. We can use triple quotes to write multiline comments. Triple quotes are used to write multiline strings in Python. If we write a multiline string but don’t assign this string to any variable, it will be treated as multiline comments. For example:
Code:
num1 = 10
num2 = 40
sum_num = num1 + num2
print(sum_num)
'''num1 and num2 are variables.
sum_num is also variable holding the
sum of both values.'''
Output:
50
We didn’t get multiline strings in output, just like comments. So basically, here we created a multiline string, but we didn’t assign any variables to this string. So it will work as comments, but it is not a comment. It is still a string. We use single quotes three times to write a multiline string. We can also use double quotes instead of single quotes.
NOTE: Always remember, according to the Python interpreter, this is not a comment. The interpreter will read it as a string, but it does not have any variables, so the interpreter will ignore it.
Now in the upper example we write a multiline string; what if we write a single line string and don’t assign it to any variable? For example:
Code:
num1 = 10
num2 = 20
'num1 and num2 consist integer values'
print(num1 + num2)
Output:
30
We didn’t get that string text in output because we simply wrote a string but didn’t assign it to any variable. And interpreters ignore it. It is still a string. It may be worked here as a comment, but remember it is a string, not a comment. Also, there is a concept named docstrings. Basically, when we write strings without assigning them to any variable, the interpreter treats them as docstrings. And the huge difference between comments and docstrings is that we can access docstrings. In the same upper example:
Code:
'num1 and num2 consist integer values'
num1 = 10
num2 = 20
print(num1 + num2)
print(__doc__)
Output:
30
num1 and num2 consist integer values
We used the built-in function _ _doc_ _
(no space between underscores) to access the docstrings. And we get the text in output. So comments cannot be accessible, but docstrings can be. However, this is not the correct docstring syntax; docstrings always used triple double-quoted strings.
Avoid Mistakes When Writing Comments
You should avoid following mistakes when writing comments:
1: Your comment should be D.R.Y. (Don’t Repeat Yourself). You don’t need to comment on a piece of code that sufficiently explains itself. For example, print (10+10) #adding two numbers. Now here, we can clearly see that we are adding two numbers. So a comment on this makes it, W.E.T. (Wrote Everything Twice). So always make sure that there is no repetition or redundancy. If a code is simple enough to understand, then it is not necessary to write a comment.
2: If you didn’t write a proper code and instead of writing correct or proper code, you use a comment to explain the code, then this is called a smelly comment. Smelly comment indicated that there is something deeper problem with your code.
3. It is possible that other people are going to read or edit your code. So it is necessary that you did not use vulgar words or rude and abusive language to write comments.
Conclusion
This is the end of the Python comments tutorial. Comments are an important part of your code. However, it is not necessary to use comments in your code. It’s a choice; if you use comments, you can make your code easily explainable to yourself or others.
End of Python Comments Tutorial.
3 thoughts on “Python Comments: What Are Comments in Python?”