Anyone Can Code.
Comments are, as the name suggests, comments on Python code. So what is the use of having comments? Well, here you go:
Now, let's get into how to make text a comment. There are mainly two ways we can create comments:
Single line comments can be created by using a hash #
before the line you want set as a
comment.
#this is a comment print("this is not a comment")
this is not a comment
Note that the #
will only comment out the line that it's used for. As you can see, the second
line still runs.
Single line comments can also be used next to regular code.
print("this is code") #this is a comment
this is code
Use single line comments to explain a particular line or give a header to a section of code
Multi line comments are used to comment out multiple lines of code or text. It is helpful for creating docstrings (to be discussed later), and also when a single line comment starts overflowing. It's particularly useful to comment out a large portion of text.
""" This is a multi line comment """ print("Follow Q-Programming on Instagram!")
Follow Q-Programming on Instagram! (Check us out!)
To create a multi line comment, enclose the text within a pair of triple quotes. They can either be
single """comment"""
. However, you cannot do this: "'"comment"'"
.
Remember, in order to be a good programmer, you need to be able to explain your code to others. That can be achieved with comments. Make it a habit to write comments while you program. This practice will be particularly helpful when you are writing large programs and need to separate your code into sections. It will also help you understand a piece of coded if you decide to leave it and go back to it later.
That's it for this tutorial! Click Next to access the next tutorial!
This site uses cookies to improve your experience as well as for site metrics. To see more, check out our Privacy Policy