Comments don’t change the outcome of a program, but they sure do make things easier! If you’re new to the beautiful programming language that is Python (Welcome!), you’ve probably never written a comment in your life. If we’re being honest, you’re probably not even sure what they’re for. But if you’re going to get better at coding, in addition to getting great web hosting, so you can get your practice building websites, you’re going to need to learn to use your Python comment function.
In this guide, we break down everything you need to know to understand Python comment, so you can look like a pro, even though you’re just starting out.
First things first:
What is Python comment?
A comment is text that doesn’t change the outcome of a code; it just lets people (or Future You) know what you’re doing with a block of code. Comments are generally useful, but they become even more important the longer your code is, the more people you’re working with, and the longer you have to leave your code sitting before you get back to it. (When you have a sixty-page program that you’ve left for a year, you can’t possibly remember the name of every single variable.)
Why use Python comment?
If you’ve only been working by yourself so far and have never put down a piece of code long enough to forget your plans for it, you probably don’t understand the importance of writing a Python comment. But when you start working on multiple projects and end up leaving strings of code languishing on your computer, things can get confusing fast. Writing your comments will help you jump back into old projects without missing a beat.
And those notes are also really helpful for anybody who collaborates with you. Instead of messaging you every five minutes to ask about your vision for your code, they can just read your comments and be fully caught up!
8 Best ways to use Python comment
There are two main reasons to add a Python comment.
1. Document your work
The first is to document what you’re doing, so you can understand your code when you read it later.
2. Explain your work
The second is to explain what you’re doing, so that other coders can understand your code.
The best practice is to write relevant comments as you’re writing your code. Here are some more things you want to bear in mind as you write your comments:
3. Comments should be specific to the line or block of code they refer to
If you don’t follow this rule, you’ll end up with a bunch of comments that are out of place, and you’ll have to spend a lot of unnecessary time trying to match comments with their relevant code snippet.
4. Comments should be short and relevant
Comments aren’t the place for rambling or for writing epistles. The point of a comment is to help people to quickly get caught up with your code. So, keep your comments to the point.
5. Comments should be included only when necessary
If a line of code is self-explanatory, there’s no need to add a comment. But if you’re declaring a variable, you can explain why you’re doing it, since that might not be immediately apparent.
6. Comment your code for loop structures
Loops can be a bit harder for other coders to understand. So, use comment blocks to explain the loop and how it fits into your plan for the rest of your code.
7. Use comments to explain functions and class methods
These are usually larger blocks of code that perform a critical function. It’s really helpful to other coders when you explain exactly what the function does and how it can be used with other parts of the software.
8. Avoid using off-color jokes or swearwords
Of course, if you’re absolutely sure that you’re the only person who’s ever going to see your code, you can include whatever comments you like. But you never know who you might want to collaborate with in the future, and you don’t want to run the risk of including embarrassing language, forgetting about it, and then accidentally showing it to a mentor. To be on the safe side, it’s best to keep your comments free of foul language.
Proper Python comment syntax
- To write a Python comment, start with the # character. For example:
#Comments in Python always start with the hash character
This works because when the interpreter encounters a # symbol anywhere, (except inside a string), it omits everything that’s written beside it until the end of the line.
- If you have more than one comment line, all of them need to start with a #. For example:
#All comments in Python start with the hash character #If there’s more than one line, add the hash to each new comment line
- To comment multiple lines, you can also use this shortcut: Hold the ctrl key and left-click everywhere you want to include a #. Next, type the # once. This will comment all the lines where you left-clicked.
You don’t need to stuff your code with comments, but there’s no denying that comments help other coders to understand your code. They even help you understand your own code if you have to leave it for a while to work on other projects. You can save yourself so much time if you keep your comments organised, and comment as you code.