|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "b3c0145c", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Python: Everything\n", |
| 9 | + "- 25) **Iterators and iterables:** \n", |
| 10 | + "<br>----------------------------------------------\n", |
| 11 | + "<br> https://www.pinterest.com/HamedShahHosseini/programming-languages/\n", |
| 12 | + "<br>https://github.com/ostad-ai/Python-Everything" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "id": "35474859", |
| 18 | + "metadata": {}, |
| 19 | + "source": [ |
| 20 | + "**Iterator:** is an object that allows us to iterate over collections of data. Therefore, the data is traversed or accessed one item at a time by an iterator.\n", |
| 21 | + "<br>An iterator class must implement two special methods **iter** and **next**.\n", |
| 22 | + "<br> **iter** is called to initialize the iterator, and it must return the iterator object.\n", |
| 23 | + "<br> **next** is called to iterate over the iterator, and must return the next value in the given data stream." |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "id": "68263de5", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "In contrast, we have iterables, which we can get an iterator from by using the iter() method. it also supports next() method, which returns the next value in the iterable.\n", |
| 32 | + "<br> Lists, sets, tuples, strings, and dictionaries are all iterables." |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "id": "8d2a1b9c", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "In the following, we define a list, and see the use of iter() and next() methods" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 1, |
| 46 | + "id": "a32fc742", |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [ |
| 49 | + { |
| 50 | + "name": "stdout", |
| 51 | + "output_type": "stream", |
| 52 | + "text": [ |
| 53 | + "hello, world, then, bye, " |
| 54 | + ] |
| 55 | + } |
| 56 | + ], |
| 57 | + "source": [ |
| 58 | + "mylist=['hello','world','then','bye']\n", |
| 59 | + "myiter=iter(mylist)\n", |
| 60 | + "while True:\n", |
| 61 | + " try:\n", |
| 62 | + " print(next(myiter),end=', ')\n", |
| 63 | + " except:\n", |
| 64 | + " break" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "id": "e8735221", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "The easy way to traverse over elements of an iterable is using for-loop" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 2, |
| 78 | + "id": "221b79d6", |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "name": "stdout", |
| 83 | + "output_type": "stream", |
| 84 | + "text": [ |
| 85 | + "hello, world, then, bye, " |
| 86 | + ] |
| 87 | + } |
| 88 | + ], |
| 89 | + "source": [ |
| 90 | + "mylist=['hello','world','then','bye']\n", |
| 91 | + "for el in mylist:\n", |
| 92 | + " print(el,end=', ')" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "markdown", |
| 97 | + "id": "1d60f725", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "Let's define an iterator class, which must implement iter() and next() methods.\n", |
| 101 | + "<br> We may have at least three different types of custom iterators:\n", |
| 102 | + "1. Take a data sequence, and return its items, one at a time\n", |
| 103 | + "2. Take a data sequence, transform each item, and return the trasnformed item, one after the other.\n", |
| 104 | + "3. Generate a new sequence of data, and return its items one after the other." |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 3, |
| 110 | + "id": "f0078a85", |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [ |
| 113 | + { |
| 114 | + "name": "stdout", |
| 115 | + "output_type": "stream", |
| 116 | + "text": [ |
| 117 | + "1, 3, 5, 7, 9, 11, 13, 15, 17, 19, " |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "# a class that generates odd numbers\n", |
| 123 | + "class oddNumbers:\n", |
| 124 | + " def __init__(self,start=1):\n", |
| 125 | + " self._number=start\n", |
| 126 | + " def __iter__(self):\n", |
| 127 | + " return self\n", |
| 128 | + " def __next__(self):\n", |
| 129 | + " item=self._number\n", |
| 130 | + " self._number+=2\n", |
| 131 | + " return item\n", |
| 132 | + "odds=iter(oddNumbers())\n", |
| 133 | + "for i in range(10):\n", |
| 134 | + " print(next(odds),end=', ')" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "id": "9f3ea82d", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "The oodNumbers class runs for ever, so if we use it in a for loop, it never ends. <br> To have a limit on the length of the sequence, we may use **StopIteration** with keyword raise." |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": 4, |
| 148 | + "id": "87da7a5d", |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "1, 3, 5, 7, 9, 11, 13, 15, 17, 19, " |
| 156 | + ] |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "# a class that generates odd numbers with a limit\n", |
| 161 | + "class oddNumbers2:\n", |
| 162 | + " def __init__(self,start=1,Nelements=10):\n", |
| 163 | + " self._number=start\n", |
| 164 | + " self._length=Nelements\n", |
| 165 | + " self._index=0\n", |
| 166 | + " def __iter__(self):\n", |
| 167 | + " return self\n", |
| 168 | + " def __next__(self):\n", |
| 169 | + " if self._index<self._length:\n", |
| 170 | + " item=self._number\n", |
| 171 | + " self._number+=2\n", |
| 172 | + " self._index+=1\n", |
| 173 | + " return item\n", |
| 174 | + " else:\n", |
| 175 | + " raise StopIteration\n", |
| 176 | + "odds2=oddNumbers2()\n", |
| 177 | + "for odd in odds2:\n", |
| 178 | + " print(odd,end=', ')" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": null, |
| 184 | + "id": "22275727", |
| 185 | + "metadata": {}, |
| 186 | + "outputs": [], |
| 187 | + "source": [] |
| 188 | + } |
| 189 | + ], |
| 190 | + "metadata": { |
| 191 | + "kernelspec": { |
| 192 | + "display_name": "Python 3 (ipykernel)", |
| 193 | + "language": "python", |
| 194 | + "name": "python3" |
| 195 | + }, |
| 196 | + "language_info": { |
| 197 | + "codemirror_mode": { |
| 198 | + "name": "ipython", |
| 199 | + "version": 3 |
| 200 | + }, |
| 201 | + "file_extension": ".py", |
| 202 | + "mimetype": "text/x-python", |
| 203 | + "name": "python", |
| 204 | + "nbconvert_exporter": "python", |
| 205 | + "pygments_lexer": "ipython3", |
| 206 | + "version": "3.11.1" |
| 207 | + } |
| 208 | + }, |
| 209 | + "nbformat": 4, |
| 210 | + "nbformat_minor": 5 |
| 211 | +} |
0 commit comments