Start learning with our library of video tutorials taught by experts. Get started
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.
Generator functions are an incredibly useful pattern in Python. What a generator function does is it actually creates an iterator. Let's take a look at how this works. We can make a working copy of generator.py. I'm going to call it generator-working.py. Go ahead and open that up and take a look. This is the generator here. Now it calls this isprime, which is very similar to the one that we used in our functions example, except instead of the Print statements, it returns False if it's not prime, and it returns True if it is prime. So that's a little utility function.
This one here, it looks, at first glance, like a normal function, and in fact, it is, except it has this yield statement. Yield is like return. It returns a value. You see here we have return False, return True. Those return those Boolean values. This one here returns a number, and it returns this number only if isprime returns true, so it returns the next prime number, but it uses yield instead of return.
And what yield does is it returns a value, but then the next time the function is called, it continues execution after the yield. So in this case, we have this while loop. And it checks to see if the number is prime. If it's not, it increments and checks the next one. And if it is, it yields. And that will return a value. And then the next time this function is called, it'll just continue here, incrementing and looking for the next one. And so this actually, because of these of yield, it returns an iterator object, that is suitable for use in a for loop.
And here we have a for loop, and for n in primes, and so it calls this primes function. There is the primes function. And for each iteration, it'll put the value in n. And it'll print it out, and it tests to see if it's greater than 100. Then it breaks out of the loop. And so this uses this generator function as an iterator. And it will print a list of prime numbers. Let's go ahead and take a look at this as it works. We'll run it.
And here we have a complete list of all the prime numbers up to 97. So you can see that this is incredibly useful. And we'll some more examples of it as we go through the course. So this is a generator function, and what it generates is an iterator that's suitable for use in a for loop, just like any iterator in the Python language.
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.