Python Comments are used in code, in order to make readable notes for yourself or someone else who might add to or edit your code in the future. Code Comments are “invisible” to the code main code, and will never be executed by the Python interpreter. Comments can also be used to prevent execution when testing code, saving you from having to delete non-working or alternative code versions in the same .py file.
NOTE: This tutorial requires that Python is already installed, as well as an IDE (Integrated Development Environment), such as PyCharm. It’s also recommended that you have a basic understanding of How to Use Python if this is your first time learning Python.
With that said, let’s get started with this free Python Comments tutorial.
How to Comment out Code in Python
To Comment code in Python, simply use the “#” (hash) symbol.
If the # is placed at the start of the line of code, the entire line will be commented out, as follows:
#This is a comment
print("I love Python")
Comments can also be added after any code, on the same line, as follows:
print("I love Python") #This is a comment
As mentioned earlier, if you are working and testing some code, you can comment out lines of code for this purpose as well:
print("I love Python")
#print("Python is easy")
Now the code statement print(“Python is easy”) is commented out and won’t be executed.
Python Code Commenting Shortcut key
While you can manually type # in front of any line of code you want to comment out in Python, a quicker method is to use the “CTRL+/” keyboard shortcut.
Simply have your text cursor on the line of code you want to comment out and press “CTRL+/”. It will now automatically comment out that line.
Note that this will always comment out the entire line of code and cannot only do sections of it in one line. You can however still do this manually as shown above.
Multi-line Comments in Python
In other coding languages such as C#, CSS, and C++, commenting out code is done by using /* */
If you want to therefore comment out multi-lines of code in these languages, you simply put the /* at the beginning and */ at the end, as follows.
/*
This is a multi-line comment
where I want to comment out
more than one line of code in C#
*/
However, in Python, code lines can only be commented out one at a time when using the # symbol. So, if you want to comment out 3 lines of code for example, each line requires a # in front of it.
#This is a multi-line comment
#where I want to comment out
#more than one line of code in Python
print("Python is easy")
As you can see, the Python method is more of a tedious way of commenting out multiple lines of code, compared to other coding languages.
While there are only 3 lines in our example, think about how much manual effort this requires if you are wanting to comment out 50 lines of code.
Multi-Line Code Commenting Shortcut key
To speed things up, we can once again make use of our “CTRL+/” shortcut key to comment out as many lines of code as we want. Simply highlight the lines of code you want to comment out using your mouse, then press “CTRL+/”.
Now all those lines of code will have a # inserted in front of them.
# apples = 6
# oranges = 9
# if apples < oranges:
# print("We need more apples!")
# else:
# print("We need more oranges!")
# print("Ok, I'll go get some")
Triple Quotes Commenting Method
Another alternative method of multi-line commenting is using the Triple quotations “”” “””.
This is the same methodology as with the other coding languages, where you place one set of triple quotes “”” before your first line and another set after the last line.
Now, everything in between will be commented out:
"""
apples = 6
oranges = 9
if apples < oranges:
print("We need more apples!")
else:
print("We need more oranges!")
print("Ok, I'll go get some")
"""
How to Comment in Python Conclusion
I hope you enjoyed this tutorial on how to comment out code in Python. We first looked into why being able to comment out code is important, not just in Python, but every other coding language.
From there we divided a little deeper into how to comment out a single line of Python code, as well as using the “CTRL+/” keyboard shortcut to speed up the process.
Using this knowledge, we could now also comment out multiple lines of code by simply highlighting them and using the shortcut key.
Lastly we looked into an alternative method, known as the “Triple Quote” method to also comment out multiple lines of code at once.
Now that you understand the fundamentals of how to comment out Python code, check out my tutorial on how to use Python Indentation or to find out more about Data Types in Python.
If you enjoyed this content and would like others to benefit from it as well, make sure to share it on your favorite Social Media platform, using the share buttons below.