├── Iterators.ipynb └── README.md /Iterators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "2d9e8884", 6 | "metadata": {}, 7 | "source": [ 8 | "## What is an Iteration\n", 9 | "\n", 10 | "Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 19, 16 | "id": "26fadf7e", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "1\n", 24 | "2\n", 25 | "3\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "# Example\n", 31 | "num = [1,2,3]\n", 32 | "\n", 33 | "for i in num:\n", 34 | " print(i)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "c7ea3a1a", 40 | "metadata": {}, 41 | "source": [ 42 | "## What is Iterator\n", 43 | "\n", 44 | "An Iterator is an object that allows the programmer to traverse through a sequence of data without having to store the entire data in the memory" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 28, 50 | "id": "ff765a6c", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "1369.0\n", 58 | "0.75\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "# Example\n", 64 | "L = [x for x in range(1,10000)]\n", 65 | "\n", 66 | "#for i in L:\n", 67 | " #print(i*2)\n", 68 | " \n", 69 | "import sys\n", 70 | "\n", 71 | "print(sys.getsizeof(L)/64)\n", 72 | "\n", 73 | "x = range(1,10000000000)\n", 74 | "\n", 75 | "#for i in x:\n", 76 | " #print(i*2)\n", 77 | " \n", 78 | "print(sys.getsizeof(x)/64)\n", 79 | "\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "99fff65f", 85 | "metadata": {}, 86 | "source": [ 87 | "## What is Iterable\n", 88 | "Iterable is an object, which one can iterate over\n", 89 | "\n", 90 | " It generates an Iterator when passed to iter() method." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 32, 96 | "id": "77650fa6", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "list_iterator" 103 | ] 104 | }, 105 | "execution_count": 32, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "# Example\n", 112 | "\n", 113 | "L = [1,2,3]\n", 114 | "type(L)\n", 115 | "\n", 116 | "\n", 117 | "# L is an iterable\n", 118 | "type(iter(L))\n", 119 | "\n", 120 | "# iter(L) --> iterator" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "id": "f9482d80", 126 | "metadata": {}, 127 | "source": [ 128 | "## Point to remember\n", 129 | "\n", 130 | "- Every **Iterator** is also and **Iterable**\n", 131 | "- Not all **Iterables** are **Iterators**" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "id": "9c4c52a7", 137 | "metadata": {}, 138 | "source": [ 139 | "## Trick\n", 140 | "- Every Iterable has an **iter function**\n", 141 | "- Every Iterator has both **iter function** as well as a **next function**" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 35, 147 | "id": "802be65a", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "['__abs__',\n", 154 | " '__add__',\n", 155 | " '__and__',\n", 156 | " '__bool__',\n", 157 | " '__ceil__',\n", 158 | " '__class__',\n", 159 | " '__delattr__',\n", 160 | " '__dir__',\n", 161 | " '__divmod__',\n", 162 | " '__doc__',\n", 163 | " '__eq__',\n", 164 | " '__float__',\n", 165 | " '__floor__',\n", 166 | " '__floordiv__',\n", 167 | " '__format__',\n", 168 | " '__ge__',\n", 169 | " '__getattribute__',\n", 170 | " '__getnewargs__',\n", 171 | " '__gt__',\n", 172 | " '__hash__',\n", 173 | " '__index__',\n", 174 | " '__init__',\n", 175 | " '__init_subclass__',\n", 176 | " '__int__',\n", 177 | " '__invert__',\n", 178 | " '__le__',\n", 179 | " '__lshift__',\n", 180 | " '__lt__',\n", 181 | " '__mod__',\n", 182 | " '__mul__',\n", 183 | " '__ne__',\n", 184 | " '__neg__',\n", 185 | " '__new__',\n", 186 | " '__or__',\n", 187 | " '__pos__',\n", 188 | " '__pow__',\n", 189 | " '__radd__',\n", 190 | " '__rand__',\n", 191 | " '__rdivmod__',\n", 192 | " '__reduce__',\n", 193 | " '__reduce_ex__',\n", 194 | " '__repr__',\n", 195 | " '__rfloordiv__',\n", 196 | " '__rlshift__',\n", 197 | " '__rmod__',\n", 198 | " '__rmul__',\n", 199 | " '__ror__',\n", 200 | " '__round__',\n", 201 | " '__rpow__',\n", 202 | " '__rrshift__',\n", 203 | " '__rshift__',\n", 204 | " '__rsub__',\n", 205 | " '__rtruediv__',\n", 206 | " '__rxor__',\n", 207 | " '__setattr__',\n", 208 | " '__sizeof__',\n", 209 | " '__str__',\n", 210 | " '__sub__',\n", 211 | " '__subclasshook__',\n", 212 | " '__truediv__',\n", 213 | " '__trunc__',\n", 214 | " '__xor__',\n", 215 | " 'as_integer_ratio',\n", 216 | " 'bit_length',\n", 217 | " 'conjugate',\n", 218 | " 'denominator',\n", 219 | " 'from_bytes',\n", 220 | " 'imag',\n", 221 | " 'numerator',\n", 222 | " 'real',\n", 223 | " 'to_bytes']" 224 | ] 225 | }, 226 | "execution_count": 35, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "source": [ 232 | "a = 2\n", 233 | "a\n", 234 | "\n", 235 | "#for i in a:\n", 236 | " #print(i)\n", 237 | " \n", 238 | "dir(a)" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 38, 244 | "id": "2daf8b0a", 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "['__class__',\n", 251 | " '__contains__',\n", 252 | " '__delattr__',\n", 253 | " '__delitem__',\n", 254 | " '__dir__',\n", 255 | " '__doc__',\n", 256 | " '__eq__',\n", 257 | " '__format__',\n", 258 | " '__ge__',\n", 259 | " '__getattribute__',\n", 260 | " '__getitem__',\n", 261 | " '__gt__',\n", 262 | " '__hash__',\n", 263 | " '__init__',\n", 264 | " '__init_subclass__',\n", 265 | " '__iter__',\n", 266 | " '__le__',\n", 267 | " '__len__',\n", 268 | " '__lt__',\n", 269 | " '__ne__',\n", 270 | " '__new__',\n", 271 | " '__reduce__',\n", 272 | " '__reduce_ex__',\n", 273 | " '__repr__',\n", 274 | " '__reversed__',\n", 275 | " '__setattr__',\n", 276 | " '__setitem__',\n", 277 | " '__sizeof__',\n", 278 | " '__str__',\n", 279 | " '__subclasshook__',\n", 280 | " 'clear',\n", 281 | " 'copy',\n", 282 | " 'fromkeys',\n", 283 | " 'get',\n", 284 | " 'items',\n", 285 | " 'keys',\n", 286 | " 'pop',\n", 287 | " 'popitem',\n", 288 | " 'setdefault',\n", 289 | " 'update',\n", 290 | " 'values']" 291 | ] 292 | }, 293 | "execution_count": 38, 294 | "metadata": {}, 295 | "output_type": "execute_result" 296 | } 297 | ], 298 | "source": [ 299 | "T = {1:2,3:4}\n", 300 | "dir(T)" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 41, 306 | "id": "7468f8bf", 307 | "metadata": {}, 308 | "outputs": [ 309 | { 310 | "data": { 311 | "text/plain": [ 312 | "['__class__',\n", 313 | " '__delattr__',\n", 314 | " '__dir__',\n", 315 | " '__doc__',\n", 316 | " '__eq__',\n", 317 | " '__format__',\n", 318 | " '__ge__',\n", 319 | " '__getattribute__',\n", 320 | " '__gt__',\n", 321 | " '__hash__',\n", 322 | " '__init__',\n", 323 | " '__init_subclass__',\n", 324 | " '__iter__',\n", 325 | " '__le__',\n", 326 | " '__length_hint__',\n", 327 | " '__lt__',\n", 328 | " '__ne__',\n", 329 | " '__new__',\n", 330 | " '__next__',\n", 331 | " '__reduce__',\n", 332 | " '__reduce_ex__',\n", 333 | " '__repr__',\n", 334 | " '__setattr__',\n", 335 | " '__setstate__',\n", 336 | " '__sizeof__',\n", 337 | " '__str__',\n", 338 | " '__subclasshook__']" 339 | ] 340 | }, 341 | "execution_count": 41, 342 | "metadata": {}, 343 | "output_type": "execute_result" 344 | } 345 | ], 346 | "source": [ 347 | "L = [1,2,3]\n", 348 | "\n", 349 | "# L is not an iterator\n", 350 | "iter_L = iter(L)\n", 351 | "\n", 352 | "# iter_L is an iterator" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "id": "5f02c4b7", 358 | "metadata": {}, 359 | "source": [ 360 | "## Understanding how for loop works" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 42, 366 | "id": "8b518b08", 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "name": "stdout", 371 | "output_type": "stream", 372 | "text": [ 373 | "1\n", 374 | "2\n", 375 | "3\n" 376 | ] 377 | } 378 | ], 379 | "source": [ 380 | "num = [1,2,3]\n", 381 | "\n", 382 | "for i in num:\n", 383 | " print(i)" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 47, 389 | "id": "bfb4f98d", 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "ename": "StopIteration", 394 | "evalue": "", 395 | "output_type": "error", 396 | "traceback": [ 397 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 398 | "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", 399 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miter_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miter_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miter_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 400 | "\u001b[1;31mStopIteration\u001b[0m: " 401 | ] 402 | } 403 | ], 404 | "source": [ 405 | "num = [1,2,3]\n", 406 | "\n", 407 | "# fetch the iterator\n", 408 | "iter_num = iter(num)\n", 409 | "\n", 410 | "# step2 --> next\n", 411 | "next(iter_num)\n", 412 | "next(iter_num)\n", 413 | "next(iter_num)\n", 414 | "next(iter_num)" 415 | ] 416 | }, 417 | { 418 | "cell_type": "markdown", 419 | "id": "e35d6e87", 420 | "metadata": {}, 421 | "source": [ 422 | "## Making our own for loop" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 48, 428 | "id": "7822da8e", 429 | "metadata": {}, 430 | "outputs": [], 431 | "source": [ 432 | "def mera_khudka_for_loop(iterable):\n", 433 | " \n", 434 | " iterator = iter(iterable)\n", 435 | " \n", 436 | " while True:\n", 437 | " \n", 438 | " try:\n", 439 | " print(next(iterator))\n", 440 | " except StopIteration:\n", 441 | " break " 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": 53, 447 | "id": "68a428be", 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "name": "stdout", 452 | "output_type": "stream", 453 | "text": [ 454 | "0\n", 455 | "1\n" 456 | ] 457 | } 458 | ], 459 | "source": [ 460 | "a = [1,2,3]\n", 461 | "b = range(1,11)\n", 462 | "c = (1,2,3)\n", 463 | "d = {1,2,3}\n", 464 | "e = {0:1,1:1}\n", 465 | "\n", 466 | "mera_khudka_for_loop(e)" 467 | ] 468 | }, 469 | { 470 | "cell_type": "markdown", 471 | "id": "81a3e616", 472 | "metadata": {}, 473 | "source": [ 474 | "## A confusing point" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 64, 480 | "id": "269d3c83", 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "name": "stdout", 485 | "output_type": "stream", 486 | "text": [ 487 | "2280889893936 Address of iterator 1\n", 488 | "2280889893936 Address of iterator 2\n" 489 | ] 490 | } 491 | ], 492 | "source": [ 493 | "num = [1,2,3]\n", 494 | "iter_obj = iter(num)\n", 495 | "\n", 496 | "print(id(iter_obj),'Address of iterator 1')\n", 497 | "\n", 498 | "iter_obj2 = iter(iter_obj)\n", 499 | "print(id(iter_obj2),'Address of iterator 2')" 500 | ] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "id": "051af6bc", 505 | "metadata": {}, 506 | "source": [ 507 | "## Let's create our own range() function" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 67, 513 | "id": "7eaf1228", 514 | "metadata": {}, 515 | "outputs": [], 516 | "source": [ 517 | "class mera_range:\n", 518 | " \n", 519 | " def __init__(self,start,end):\n", 520 | " self.start = start\n", 521 | " self.end = end\n", 522 | " \n", 523 | " def __iter__(self):\n", 524 | " return mera_range_iterator(self)" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 68, 530 | "id": "28118f2d", 531 | "metadata": {}, 532 | "outputs": [], 533 | "source": [ 534 | "class mera_range_iterator:\n", 535 | " \n", 536 | " def __init__(self,iterable_obj):\n", 537 | " self.iterable = iterable_obj\n", 538 | " \n", 539 | " def __iter__(self):\n", 540 | " return self\n", 541 | " \n", 542 | " def __next__(self):\n", 543 | " \n", 544 | " if self.iterable.start >= self.iterable.end:\n", 545 | " raise StopIteration\n", 546 | " \n", 547 | " current = self.iterable.start\n", 548 | " self.iterable.start+=1\n", 549 | " return current" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 70, 555 | "id": "142c0b98", 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "x = mera_range(1,11)" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 71, 565 | "id": "5aaaeeb3", 566 | "metadata": {}, 567 | "outputs": [ 568 | { 569 | "data": { 570 | "text/plain": [ 571 | "__main__.mera_range" 572 | ] 573 | }, 574 | "execution_count": 71, 575 | "metadata": {}, 576 | "output_type": "execute_result" 577 | } 578 | ], 579 | "source": [ 580 | "type(x)" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 72, 586 | "id": "8e742864", 587 | "metadata": {}, 588 | "outputs": [ 589 | { 590 | "data": { 591 | "text/plain": [ 592 | "<__main__.mera_range_iterator at 0x2130fd362b0>" 593 | ] 594 | }, 595 | "execution_count": 72, 596 | "metadata": {}, 597 | "output_type": "execute_result" 598 | } 599 | ], 600 | "source": [ 601 | "iter(x)" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": null, 607 | "id": "95380d0b", 608 | "metadata": {}, 609 | "outputs": [], 610 | "source": [] 611 | } 612 | ], 613 | "metadata": { 614 | "kernelspec": { 615 | "display_name": "Python 3", 616 | "language": "python", 617 | "name": "python3" 618 | }, 619 | "language_info": { 620 | "codemirror_mode": { 621 | "name": "ipython", 622 | "version": 3 623 | }, 624 | "file_extension": ".py", 625 | "mimetype": "text/x-python", 626 | "name": "python", 627 | "nbconvert_exporter": "python", 628 | "pygments_lexer": "ipython3", 629 | "version": "3.8.8" 630 | } 631 | }, 632 | "nbformat": 4, 633 | "nbformat_minor": 5 634 | } 635 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-iterators-and-iterables 2 | Demo code for python iterators and iterables 3 | --------------------------------------------------------------------------------