{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "22cfb37d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   i=0, x=<generator object yieldTest at 0x7f49781ff820>\n",
      "   i=1, x=<generator object yieldTest at 0x7f49781ff970>\n",
      "   i=2, x=<generator object yieldTest at 0x7f49781ff740>\n",
      "   i=3, x=<generator object yieldTest at 0x7f49781ffac0>\n"
     ]
    }
   ],
   "source": [
    "# The yield statement suspends function’s execution and \n",
    "# sends a value back to the caller, **but** retains enough state \n",
    "# to enable function to **resume** where it is left off. \n",
    "# When resumed, the function continues execution immediately \n",
    "# after the last yield run. \n",
    "# This allows its code to produce a series of values over time, \n",
    "# rather than computing them at once and sending them back like a list.\n",
    "\n",
    "def yieldTest():\n",
    "    yield 1\n",
    "    yield 2\n",
    "    yield 3\n",
    "   \n",
    "# Test\n",
    "for i in list(range(4)):\n",
    "    x = yieldTest()\n",
    "    print(f\"   i={i}, x={x}\")\n",
    "    \n",
    "# When a function is called and the thread of execution finds \n",
    "# a yield keyword in the function, the function execution \n",
    "# stops at that line itself and it **returns a generator object*\n",
    "# back to the caller. (generator is like an iterator !!!)\n",
    "#\n",
    "# Generators are functions that return an **iterable generator object**. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c7f186c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i=1, i^2=1\n",
      "i=2, i^2=4\n",
      "i=3, i^2=9\n"
     ]
    }
   ],
   "source": [
    "###############################################\n",
    "# LOKS LIKE IT ONLY WORKS in a FOR loop !!!\n",
    "###############################################\n",
    "for i in yieldTest():\n",
    "    x=i*i\n",
    "    print(f\"i={i}, i^2={x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d6304237",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "9\n",
      "16\n",
      "25\n",
      "36\n",
      "49\n",
      "64\n",
      "81\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "# A Python program to generate squares from 1\n",
    "# to 100 using yield and therefore generator\n",
    "  \n",
    "# An infinite generator function that prints\n",
    "# next square number. It starts with 1\n",
    "def nextSquare():\n",
    "    i = 1\n",
    "  \n",
    "    # An Infinite loop to generate squares \n",
    "    while True:\n",
    "        yield i*i                \n",
    "        i += 1  # Next execution resumes \n",
    "                # from this point     \n",
    "  \n",
    "# Driver code to test above generator \n",
    "# function\n",
    "for num in nextSquare():\n",
    "    if num > 100:\n",
    "         break    \n",
    "    print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4333ce18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# How to read the values from the generator\n",
    "\n",
    "# (1) Using : list()\n",
    "x = list(yieldTest())\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e2e21ca6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# (2) Using : for-in\n",
    "for i in yieldTest():\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b40f5c32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    },
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "Input \u001b[0;32mIn [22]\u001b[0m, in \u001b[0;36m<cell line: 8>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(x))\n\u001b[1;32m      7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(x))\n\u001b[0;32m----> 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m)\n",
      "\u001b[0;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# Using next()\n",
    "\n",
    "x = yieldTest()\n",
    "\n",
    "print(next(x))\n",
    "print(next(x))\n",
    "print(next(x))\n",
    "print(next(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8ff9f1b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# NOTE: Generators are one-time Use\n",
    "\n",
    "# If you try to use them again, it will be empty.\n",
    "\n",
    "x = yieldTest()\n",
    "print(list(x))\n",
    "print(list(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d36b23a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "# To get a new copy, you will have to make the call to function again.\n",
    "x = yieldTest()\n",
    "print(list(x))\n",
    "x = yieldTest()\n",
    "print(list(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e02a0dae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
