|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# <center>Intermediate Python (Part-1)</center>" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "# ***<center>Iterators and Generators</center>***\n", |
| 15 | + "\n", |
| 16 | + "<img src=https://i.imgur.com/e1Deq4a.jpg height=300 width=300>" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "## 1. Iteration protocol in Python\n", |
| 24 | + "\n", |
| 25 | + "\n", |
| 26 | + "- **Iteration:** repitition of a process.\n", |
| 27 | + "- **Iterable:** a Python object which supports iteration.\n", |
| 28 | + "- **Iterator:** a Python object to perform iteration over an iterable." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "### Iteration Protocol in Python\n", |
| 43 | + "\n", |
| 44 | + "The **iteration protocol** is a fancy term meaning “how iterables actually work in Python”.\n", |
| 45 | + "\n", |
| 46 | + "1. For a class object to be an Iterable:\n", |
| 47 | + " - Can be passed to the iter function to get an iterator for them.\n", |
| 48 | + "\n", |
| 49 | + "2. For any Iterator:\n", |
| 50 | + " - Can be passed to the next function which gives their next item or raises StopIteration\n", |
| 51 | + " - Return themselves when passed to the iter function." |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "class Iterable:\n", |
| 68 | + " def __init__ (self):\n", |
| 69 | + " " |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "markdown", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "## 2. Generators\n", |
| 77 | + "\n", |
| 78 | + "Simple **functions** or **expressions** used to create iterator.\n", |
| 79 | + "\n", |
| 80 | + "Let's write a function which return the factorial of first 10 natural numbers." |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 22, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [], |
| 88 | + "source": [ |
| 89 | + "def factorial(n):\n", |
| 90 | + " fact = []\n", |
| 91 | + " k = 1\n", |
| 92 | + " for i in range(1,n+1):\n", |
| 93 | + " k *= i\n", |
| 94 | + " fact.append(k)\n", |
| 95 | + " return fact" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "Let's make it memory efficient using generators!" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "### Generator expression\n", |
| 124 | + "\n", |
| 125 | + "Now, let us find the sum of squares of first 10 natural numbers, but this time, without any function!" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "This can also be converted into a generator **expression**!" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": null, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "# Let's sum up it all!\n", |
| 154 | + "" |
| 155 | + ] |
| 156 | + } |
| 157 | + ], |
| 158 | + "metadata": { |
| 159 | + "kernelspec": { |
| 160 | + "display_name": "Python 3", |
| 161 | + "language": "python", |
| 162 | + "name": "python3" |
| 163 | + }, |
| 164 | + "language_info": { |
| 165 | + "codemirror_mode": { |
| 166 | + "name": "ipython", |
| 167 | + "version": 3 |
| 168 | + }, |
| 169 | + "file_extension": ".py", |
| 170 | + "mimetype": "text/x-python", |
| 171 | + "name": "python", |
| 172 | + "nbconvert_exporter": "python", |
| 173 | + "pygments_lexer": "ipython3", |
| 174 | + "version": "3.6.3" |
| 175 | + } |
| 176 | + }, |
| 177 | + "nbformat": 4, |
| 178 | + "nbformat_minor": 2 |
| 179 | +} |
0 commit comments