Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Comments in Python are quite simple. Let's take a look. We'll start by making a working copy of comments.py, and we'll call it comments-working.py. We'll go ahead and open the working copy. And here we have a little program that generates a list of prime numbers. Now, you'll notice that at the top of the file, we have a number of lines that are introduced by a Pound sign. I call it a Pound sign some people called the Hash Mark, or that tick-tack-toe- looking thingie, whatever we call it.
That is the symbol that indicates a comment in Python. So everything starting with the Pound sign, and all the way to the end of the line, is ignored by the Python interpreter. So all these are comments. You'll notice that we don't have any comments in the rest of the code. And this is a piece of code that may very well benefit from some comments. So for example, this line here, I might have a comment that says # generate a list of prime numbers.
And now when somebody comes back and looks at the code later on he'll, see this: for n in primes generate a list of prime numbers. Oh, well I guess that's what that does. Now, when you're reading comments you want to be careful that you don't just trust the comment, that you actually look at the code and make sure that the code actually does what you think it does. Use the comment as a guideline, but oftentimes as people are writing code they might put in a comment and then maybe change the code later, forget to change the comment, or perhaps their terminology is a little bit different than what you're expecting.
So comments are very useful. The purpose of comments is to make the code more readable by human beings, because something like this here, I wrote it, so I know what it does, but you're looking at it and you might say, hmm, what algorithm is he using to generate these prime numbers? And so a couple of little comments might make it a lot more readable. For example, I can say # one is never prime by definition. And over here I might say # found a divisor, not prime.
And over here, because we have a generator function and a lot of people who aren't familiar with Python might not know what a generator function is, I can say, # yield makes this a generator. And it'll at least give somebody a clue. They can look that up if they don't know what that means. So the principle here is to use comments to make the code a little bit more clear. There is a danger, of course, if you use too many comments, if you comment every line, or if you comment way too many things.
The comments might become a distraction. But just keep in mind that the purpose of the comment is to make the code more clear to somebody who is reading it for the first time and may not be familiar with the algorithm that you are using. So comments in Python are introduced by a Pound sign or Hash Mark. And everything from that Pound sign to the end of the line is ignored by the interpreter, and therefore considered a comment.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104480 Viewers
56 Video lessons · 116396 Viewers
71 Video lessons · 85619 Viewers
131 Video lessons · 40982 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.