Comments
Python Basics • Last Updated: 01/12/2025
Python Basics • Last Updated: 01/12/2025
Comments in Python are a simple but useful way to organise your code. What if others want to edit it or use it? Well, comments are easy to use, and they can be used anywhere in your code. They don't have to be multi-line but can be (jump to section). Comments are the way to organise code as even just little notes can help make it so much more readable.
Comments can easily be created with a hashtag symbol (#). It's as simple as that. Just remember to leave a space immediately after the hashtag and write your comment. One important thing to note is that anything on a line after the hashtag and space is all comment, that is, any code you try to write on a line after defining a comment will not work.
Here is an example of how you could use comments in your code:
... notice how the comment is greyed out? All Python editors do this, as comments do not affect your code. Also, when you actually run your code, comments will not show up. Comments are simply a visual quirk in the editor that you organise your code with.
TIP: If you really felt like it, you could also use your comments as dividers to separate different sections in your code -- it works, AND it doesn't obstruct your code.
What about comments that span across multiple lines of code? Well, the simplest way to do it is just having the comment tag on each line, as it is the most efficient and uses the least amount of processing power:
There's also the triple-quote method, which involves using a string denoted by three quotes (double or single, but only one for the whole comment), and this only works if you choose to not apply it to a variable. The issue with this method however is that it actually does use extra processing power, so we don't recommend that you use it a lot in your projects.
One thing useful about multi-line comments made this way is that you can temporarily disable any code by just having the triple quotations on the line above, and on the line below. This in practical use with a print() command looks like this:
... as a result however, you will see that the whole section there has become a string, further denoted by the green syntax colouring. Either way, this is a fairly efficient way to disable code without deleting it, as this can be used to debug any code that might've not run the way you would have liked it to.
To be fair, there isn't much to be said about comments in Python other than that they're just notes you can place in your code, to make it more readable for both yourself, or others. Comments can be really handy if you want to make readable code, for future sessions to come.
Multiline comments exist, but there are two ways to do it, one is clean and easy, but the other is more commonly used for debugging -- so it's not recommended to straight up keep.
If you have any questions, please head to the comments section below!
Share your opinions or ask questions.
-