Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
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.
Find answers to the most frequently asked questions about Python 3 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.