When you want to operate on each value of a container or a sequence, the Python for loop provides a simple and elegant way to do that.
- [Bill] The for loop uses a sequence like an iterator list…to pull our other sequence object to control the loop.…Here in Komodo,…I've opened a working copy of for.py…from chapter six of the exercise files,…and here we have our for loop,…which uses this to pull to supply its items,…and it assigns each item to the variable pet,…and then prints it.…And so when I run this, you see we get,…open this up a little bit so we get the whole thing,…we get our five items from the to pull,…bear, bunny, dog, cat, and velociraptor,…all cute little animals.…
So obviously this can be any sequence,…it can be a range is very common,…you can say range five and that…actually supplies an iterator,…and so when I save and run,…we get zero, one, two, three, and four,…or any sequence or any iterator.…This is extremely powerful and useful and convenient,…and you'll see it often.…In many cases, you'll see it more often than the while loop.…I know that I tend to use this a lot more often…than I use a while loop in my code.…
I'll show you additional ways to control the for loop…
- 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 Efficientlywith 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.