Previous Lesson: Lesson 7: Operators and Delimiters in Python
In this lesson, we’ll make comments about commenting in Python using the octothorpe or hashtag (#) and quotes (””” and “”””””). When you’re reading your code with comments on many lines, you’ll know what that line of code does.
Let’s just open our ex2.py file. Hopefully, you still have it with you. We’ll do something incredible in it and you’ll see how we’ll be commenting in Python using the hashtag and quotes. We’ll make sure that our code still runs fine:
Commenting Using the Octothorpe or Hashtag(#)
Note: ‘#‘ is called an octothorpe. And it’s also known as a hashtag.
Now, make your ex2.py look exactly like mine:
Now, when we run this, we should not see comments; they should not get printed. That’s what commenting is–you just comment. On Mac, this is what my Terminal spat out:
Nice and neat. If you have an error, double-check. Remember: Read your code from bottom to top; read every character aloud. Especially this time, when you encounter the #, say “octothorpe!” so that you’ll remember this symbol.
Commenting Using Quotes
But what if we want to write a lot of comments, that our project turns into a commenting project instead of a coding project? If you really want to write comments on that many lines, you can use three pairs of double-quotes or three pairs of single-quotes. Like what we did on Line 29 down to Line 33:
Why do we use this anyway? That’s because if a time comes when we’re not sure whether to keep something or if we would be using it for later. So to prevent that chunk of code from being completely gone out from history, like that on Line 30 down to Line 32, we can do that. Or, we can also do three pairs of double-quotes:
The outcome should be the same. However, if it’s not the same, you may have missed something or forgot something. Double check and then run it until it still looks like this:
Comments are important so that when you read your code or when you check it, you may have comments where you can say what that piece of line in your code does and everything. We’re going to use these very often.
Talking about comments, leave a comment down on the Comments section and tell me everything that you want to tell me.