{ "cells": [ { "cell_type": "markdown", "id": "ccfd6770", "metadata": {}, "source": [ "# Python Basics - 09. Comprehensions, Iterators, and Generators\n", "\n", "This notebook introduces Pythonic ways to transform data efficiently. You will learn list/dict/set comprehensions, how iteration works under the hood, and how generators help you process large data lazily." ] }, { "cell_type": "markdown", "id": "cd3a2989", "metadata": { "language": "markdown" }, "source": [ "## Download Notebook\n", "\n", "{download}`Download this notebook <09_comprehensions_and_generators.ipynb>`\n" ] }, { "cell_type": "markdown", "id": "22839646", "metadata": {}, "source": [ "## 1. List Comprehensions\n", "\n", "A list comprehension is a concise way to create a list from another iterable." ] }, { "cell_type": "code", "execution_count": null, "id": "f114c78c", "metadata": {}, "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5, 6]\n", "squares = [n * n for n in numbers]\n", "even_squares = [n * n for n in numbers if n % 2 == 0]\n", "\n", "print(f\"Numbers: {numbers}\")\n", "print(f\"Squares: {squares}\")\n", "print(f\"Even squares: {even_squares}\")" ] }, { "cell_type": "markdown", "id": "c041e8b1", "metadata": {}, "source": [ "## 2. Dict and Set Comprehensions\n", "\n", "The same pattern can build dictionaries and sets in one readable expression." ] }, { "cell_type": "code", "execution_count": null, "id": "582db8b9", "metadata": {}, "outputs": [], "source": [ "words = [\"python\", \"is\", \"clean\", \"and\", \"powerful\"]\n", "length_map = {word: len(word) for word in words}\n", "first_letters = {word[0] for word in words}\n", "\n", "print(f\"Length map: {length_map}\")\n", "print(f\"First letters set: {first_letters}\")" ] }, { "cell_type": "markdown", "id": "0df2ab1c", "metadata": {}, "source": [ "## 3. Iterators and the Iterator Protocol\n", "\n", "Any iterable can return an iterator with `iter()`. The iterator yields values one by one with `next()` until `StopIteration`." ] }, { "cell_type": "code", "execution_count": null, "id": "fbbabe46", "metadata": {}, "outputs": [], "source": [ "colors = [\"red\", \"green\", \"blue\"]\n", "it = iter(colors)\n", "\n", "print(next(it))\n", "print(next(it))\n", "print(next(it))\n", "\n", "# next(it) # Uncomment to raise StopIteration" ] }, { "cell_type": "markdown", "id": "05be2a4b", "metadata": {}, "source": [ "## 4. Generator Expressions\n", "\n", "A generator expression looks like a list comprehension but uses parentheses. It computes values lazily, so memory usage is smaller." ] }, { "cell_type": "code", "execution_count": null, "id": "687f3c64", "metadata": {}, "outputs": [], "source": [ "big_range = range(1, 1_000_001)\n", "sum_of_squares = sum(n * n for n in big_range)\n", "print(f\"Sum of squares from 1..1,000,000: {sum_of_squares}\")" ] }, { "cell_type": "markdown", "id": "1a2f887d", "metadata": {}, "source": [ "## 5. Generator Functions with `yield`\n", "\n", "Use `yield` to produce a sequence over time. The function pauses between values and resumes where it stopped." ] }, { "cell_type": "code", "execution_count": null, "id": "12e1b274", "metadata": {}, "outputs": [], "source": [ "def fibonacci(limit):\n", " a, b = 0, 1\n", " while a <= limit:\n", " yield a\n", " a, b = b, a + b\n", "\n", "print(list(fibonacci(50)))" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }