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.
There are two fundamentals types of loops in Python. There is while loops and for loops, and let's start by looking at the while loop. So we'll take our whileloop.py and make a little copy of it, and call it whileloop-working. Go ahead and open that, and you'll see this is a simple Fibonacci series. It's a common exercise for while loops. So here we are assigning two values to a and b: 0 and 1. The 0 will get assigned to a, and the 1 will get assigned to b. We say while b < 50 and we go ahead and do things we'll make b grow in a Fibonacci series and we print it each time using the print function.
So if I go ahead and run this and select the Python Run, you'll see here we get the Fibonacci series. So while loops just work like that. They say while, and then there is a condition. And as long as this condition is true, the while loop will continue to execute. And as soon as this condition becomes not true, then the while loop will end and execution will continue after it if there is any code after it, which in this case we don't have. Down here I could say print ("Done") and if I save that and run it, you'll see we get Done at the end.
You'll notice that the print ("Done") is not indented. If I were to indent it here, then it would become part of the loop. But by having it back here, at the same indention level as the while statement itself, then it is no longer part of this suite of code that is controlled by the while loop. So that's how while loops work. Let's take a look at for loops. Go ahead and make a copy of forloop.py, and I'll name it forloop-working and we'll open that.
What a for loop does is it works with what's called an iterator. An iterator is an object that each time you call it, it gives you the next value. So in this case, we open a file and it's this lines.txt, which we can see right here; it just has five lines of text. Then for line in fh, that's the file handle, which is actually an object and it has this method readlines, which is an iterator.
So for each time this fh.readlines is called, it will put the next value in the line variable, which will be the next line of text, and then we'll print it. So when I run it, we get these lines of text. Now, you'll notice that there is an extra line ending, an extra new line, in each one of these, and that's because the print function puts a new line at the end, and each of these lines actually has a new line at the end of it, so we get two for each one.
If you hover the mouse in Eclipse, you hover the mouse over the print function, you get a little bit of documentation for it. You'll notice that there is an end option, and it defaults to having this new line. So if I just say end= ' ', empty string like that and I save it and run it, now, we don't have that effect anymore. So, the for loop works likes this. It calls an iterator, and its values get put in this variable here.
It has a colon, like all the control structures in Python, and the block of code underneath it is indented. So for each line of readlines, it will take the line, put it in the line variable, and then print it, just like we see here. So that's how for loops work in Python, and those are the two types of loops that we'll see in Python. We have while loops, which are traditional, conditional loops, and for loops, which work with iterators.
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.