Video: Creating sequences with generator functions

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.

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.

Creating sequences with generator functions

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 inour functions example, except instead of the Print statements, it returnsFalse if it's not prime, and it returns True if it is prime. So that's alittle utility function.

This one here, it looks, at first glance, like a normal function, and in fact, itis, 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 nextprime number, but it uses yield instead of return.

And what yield does is it returns a value, but then the next time the functionis 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 ittests 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'ssuitable for use in a for loop, just like any iterator in the Python language.

Q: The installation process for the PyDev Eclipse plug-in doesn’t work as described in the videos. What should I do?

A: The version of Eclipse used in the recording of Python 3 Essential Training was Eclipse 3.5.2 (Galileo SR2) and the version of PyDev was 1.5.7. Soon after recording, the Eclipse project was updated to version 3.6, called Helios. As of this writing, the current version of Eclipse is 3.6.1 (Helios SR1).
Around the same time as these updates were being released, the PyDev project was updated as well. As of this writing, the current version of PyDev is 1.6.2. If you are using these most recent versions, the procedure for installing Eclipse itself has not changed, but the process documented in the videos for installing PyDev may not work.

We will be releasing new versions of the videos soon, but the author has posted a document describing the new installation procedure at:
<a href="http://python.bw.org/pydev-installation/" target="blank>http://python.bw.org/pydev-installation/</a>

Q: How do you install the pydev-interpreter in Eclipse Indigo on Mac OS X Lion? It's significantly different than what's shown in the video "Installing Python 3 and Eclipse for Mac."

A: Since this course was publishing, there have been upgrades to Mac OS X and Eclipse. In this scenario, after installing Python and Eclipse and the Pydev interpreter, there is a different directory to go to when modifying the preferences. As instructed in the movie (at around 6:40) restart Eclipse and then go to Eclipse > Preferences and drill down to Pydev > Interpreter - Python in the sidebar. Click New and in the Select interpreter dialog that opens, click Browse.

The Open dialog box will open, but does not appear to display your hard drive. You must press and hold the Command+Shift+Period keys. This will display all hidden files on your system. Navigate to the new path ~/Library/Frameworks/Python.framework/versions/3.2/bin/python 3.2 and click Open.

You should be able to proceed normally from there.

Q: When I try to install PyDev, it's not showing up in the Available Software window.

A: This can happen if the pydev.org/updates site is down.

You can manually download PyDev from the pydev.org web site, or from my site here:

http://python.bw.org/

Installation is simple. I've included instructions on my web site above.

Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.