A generator is a function that serves as an iterator. It returns a sequence of values, one for each invocation of the function. This lesson shows you how to create a generator function.
- [Instructor] A generator is a special class of function…that serves as an iterator…instead of returning a single value…the generator returns a stream of values.…Here in Komodo I've opened a working copy of generator.py…from chapter seven of the exercise files…and first I want to show you the problem that…this generator is solving.…If instead of calling my function I call…range here and save and run…you notice I've specified the number 25…and in a result it counts from zero to 24,…which is 25 values starting at zero.…
I find that a little bit confusing myself at times…and so I created a version of range…that I call inclusive range.…It simply works exactly like range…only it returns all of the values from zero…all the way up to 25, so when I save and run this you see…that my result now includes the 25.…You're welcome to use which ever version of this you like,…but it's great as an illustration of how a generator works,…because that's what range is is it's a generator.…
So let's take a look at the generator function.…
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficiently (2017)with Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.