In this lesson, you’ll learn how to write comments in Python. You’ll see that comments are made by putting a “#” symbol before a desired comment. The lesson will also show you how to spread comments over multiple lines as well as how to write comments quickly in your editor using shortcuts,
How to Write Comments in Python
00:36 For a word about style, PEP 8 advises keeping each line of Python at 79 or fewer characters, while the recommendation for comments is a maximum of 72. Therefore, comments need to be short and sweet.
If you need extra room to explain your code, you might consider a multiline comment. Python does not have native start and end symbols for multiline comments like other languages do. For example, in this C++ program, we can see that the
/* and the
*/ allow us to create multiple lines of comments between them.
01:16 So if Python does not provide multiline comment syntax, how do we create comments that might span multiple lines? Well, there are two ways. The first way might be obvious, but it’s to start each line of a multiline comment with a hash symbol.
01:48 “But wait!” you say. “That’s a Python string!” And you’d be correct. This string may not technically be a comment, but it can serve the purpose of one. As a string that’s not stored or referenced by your program, it will be ignored at runtime and won’t appear in the bytecode.
02:05 But there’s a catch. This type of multiline comment, if it’s positioned in your program at certain places—let’s say, as the beginning lines of a class, module, or function declaration—will be treated as a docstring and be associated with that particular object.
Docstrings are covered more in-depth in another Real Python article, but I’ll quickly show you what I mean. Here’s a triple-quoted comment that’s positioned immediately beneath a function declaration of
As a result, this string will be read as a docstring and be associated with the
factorial() function. We can see the effect of this by passing the
factorial object to the
help() function. So yes, you will see comments that are surrounded by triple quotes, and that’s mostly okay. But just to be safe, a good rule of thumb may be to stick with the hash symbols unless creating docstrings intentionally.
03:07 Sometimes our coding editors help us out and provide us with some commenting shortcuts. Here are some examples. In some editors, I can hold down my Control, Command, or Option key while left-clicking.
03:29 Another neat trick is to select over lines of code or text and press Control or Command with / (front slash). This will prepend each line with the hash symbol, effectively commenting out that block.
03:55 I’m using the latest version of PyCharm, provided by JetBrains. I know that these shortcuts also work in Visual Studio Code, from Microsoft. Now we know why we comment and how we comment. Coming up in the next video, we’ll learn some Dos and Don’ts to keep our code readable and our comments professional. See you there.
Become a Member to join the conversation.