English subtitles

← 02-40 Nitty Gritty For Loops

Get Embed Code
1 Language

Showing Revision 1 created 04/23/2012 by Amara Bot.

  1. Now, after all that, I think you're finally ready--you're mature enough--
  2. to learn the whole truth about how for statements actually work.
  3. You've been using them all along, but you may not have known the inner details,
  4. the gory truth, about what's inside the for statement.
  5. Now, when I saw for x in items, print x,
  6. you probably have a model that looks something like this:
  7. assuming items is a list or a tuple or a string,
  8. you think of this code probably as something like i equals 0
  9. while i is less than the length of the items, x equals items at i,
  10. and then print x.
  11. That's a good model as long as items is one of these sequence types like lists.
  12. But items can also be other things, as we've seen.
  13. It can be a generator expression, for example.
  14. Overall, Python calls the thing that can be iterated over in a for loop an iterable.
  15. Strings and lists are examples of iterables, and so are generators.
  16. What actually happens when we implement this piece of code,
  17. it's as if we had written this code.
  18. First we take the items and we make an iterator from them
  19. by calling the built-in function "iter." I'm going to call that "it."
  20. Then we're going to have a while loop that goes forever.
  21. Its loop control says while True
  22. we're going to assign x to be the next item off of the iterator,
  23. then do whatever was in the body of the for loop, in this case print x.
  24. We're going to keep on doing this sequence as long as we can get an x value.
  25. But when next stops then we'll have an exception, which is a stop iteration exception,
  26. and we don't need to do anything more. We're done.
  27. That's what a for loop really means in Python.
  28. We take the items, we create an iterator over them,
  29. we call that iterator until we're done, and then we pass through to the next statement.
  30. We're finally ready to define this c, this counting function.
  31. What it does is it takes a sequence, it says this is the first time I've been called.
  32. I'm going to initialize my starts to one.
  33. Then I'm going to enter into a loop and this means that c is a generator function.
  34. The generator function will be returned, and as part of the for protocol,
  35. we'll call that generator function each time we want the next item in the sequence
  36. and each time we do that, our count of items will be incremented.
  37. When we're done, when the for loop doesn't want any more, we'll stop incrementing.
  38. We don't necessarily go through every item in the sequence.
  39. We'll just have just the right number of counts for item.
  40. This will give us the right number of starts and the right number of items.
  41. We can do that because we control the time of execution,
  42. because this is a generator function and not a regular function.