{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ch12-title",
   "metadata": {},
   "source": [
    "# Iterators & Generators"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch12-overview",
   "metadata": {},
   "source": [
    "Iterators and generators are the backbone of Python's memory-efficient data processing.\n",
    "\n",
    "- The iterator protocol: `__iter__` and `__next__`\n",
    "- Built-in lazy iterators\n",
    "- Custom iterator classes\n",
    "- Generator functions and generator expressions\n",
    "- Lazy pipelines for streaming data\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1200-video",
   "metadata": {},
   "source": [
    "<h2>Chapter Video</h2>\n",
    "\n",
    "This overview introduces generator functions and lazy value production with yield.\n",
    "\n",
    "```{raw} html\n",
    "<iframe width=\"100%\" height=\"400\" src=\"https://www.youtube-nocookie.com/embed/bD05uGo_sVI\" title=\"Python Tutorial: Generators - How to use them and the benefits you receive\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen referrerpolicy=\"strict-origin-when-cross-origin\"></iframe>\n",
    "```\n",
    "\n",
    "> *[Python Tutorial: Generators - How to use them and the benefits you receive (11:14)](https://youtube.com/watch?v=bD05uGo_sVI) by Corey Schafer*\n",
    ">\n",
    "> Hosted on YouTube; property of its original creator, embedded here for educational reference only."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch12-goals",
   "metadata": {},
   "source": [
    "<h2>Learning Objectives</h2>\n",
    "\n",
    "By the end of this chapter, you will be able to:\n",
    "\n",
    "1. Explain Python's iterator protocol and use `iter()` and `next()` manually\n",
    "2. Use built-in lazy iterators such as `enumerate()`, `zip()`, `map()`, `filter()`, and `reversed()`\n",
    "3. Build custom iterator classes that implement the iterator protocol\n",
    "4. Write generator functions with `yield`\n",
    "5. Design multi-step data pipelines using chained generators\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch12-flow",
   "metadata": {},
   "source": [
    "<h2>Chapter Flow</h2>\n",
    "\n",
    "```{container} chapter-flow\n",
    "1. {doc}`1101-iterators`\n",
    "2. {doc}`1102-generators`\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch11-glossary",
   "metadata": {},
   "source": [
    "<h2>Glossary</h2>\n",
    "\n",
    "| # | Term | Explanation |\n",
    "|---:|---|---|\n",
    "| 1 | Iterable | An object that can provide its items one at a time. |\n",
    "| 2 | Iterator | An object that returns one item at a time and remembers its position. |\n",
    "| 3 | Iterator protocol | The `__iter__` and `__next__` methods that make an object work as an iterator. |\n",
    "| 4 | `iter` | A built-in function that returns an iterator from an iterable. |\n",
    "| 5 | `next` | A built-in function that asks an iterator for its next item. |\n",
    "| 6 | `StopIteration` | The exception that signals an iterator has no more items. |\n",
    "| 7 | Lazy iterator | An iterator that produces values on demand instead of building a full list first. |\n",
    "| 8 | `enumerate` | A lazy iterator that pairs each item with its index. |\n",
    "| 9 | `zip` | A lazy iterator that pairs items from multiple iterables. |\n",
    "| 10 | `map` | A lazy iterator that applies a function to each item. |\n",
    "| 11 | `filter` | A lazy iterator that keeps items for which a function returns true. |\n",
    "| 12 | Generator function | A function that uses `yield` to produce a sequence of values lazily. |\n",
    "| 13 | `yield` | A statement that produces a value from a generator and pauses its execution. |\n",
    "| 14 | Generator expression | A compact expression that creates a lazy generator. |\n",
    "| 15 | Infinite sequence | A sequence that can keep producing values without a fixed end. |\n",
    "| 16 | `itertools` | A standard-library module with tools for efficient iterator pipelines. |\n",
    "| 17 | `islice` | An `itertools` function that takes selected positions from an iterator. |\n",
    "| 18 | `chain` | An `itertools` function that connects multiple iterables into one stream. |\n",
    "| 19 | `yield from` | Syntax for delegating part of a generator to another iterable. |\n",
    "| 20 | Async iteration | Iteration over values that may arrive asynchronously, commonly using `async for`. |\n",
    "| 21 | Generator pipeline | A chain of generators in which each stage lazily consumes the output of the previous stage. |\n",
    "| 22 | Iterator exhaustion | The state of an iterator that has produced all its items and yields nothing on later passes. |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1200-slides",
   "metadata": {},
   "source": [
    "<h2><a href=\"overview.html\" target=\"_blank\" style=\"color: var(--pst-color-link, #176de8);\">Chapter Overview Slides</a></h2>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
