{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOkPHpFofPbpRku8pw/IeWx",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"# **Benefits of Using Python Iterators**\n",
"The main advantage of using the iterators is that the program is only holding one object at a time from a sequence or a collection.\n",
"\n",
"For example, to perform an additional operation on each element of a huge list like [1334, 5534, 5345, 345, 144, ……. ]. We will only hold one value at a time to perform operations on. There is no need to keep all the elements of the list in the memory. This saves the resources of computers."
],
"metadata": {
"id": "39wwjPLLSQoL"
}
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Q8KPnVQ0SDJD",
"outputId": "57ac33af-fb42-4797-b73f-81e7f63e6e41"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"0\n",
"4\n",
"16\n",
"36\n",
"64\n",
"['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\n",
"\n",
"2\n",
"2\n",
"4\n",
"6\n",
"8\n",
"2\n",
"4\n",
"6\n",
"8\n",
"2\n",
"4\n",
"6\n",
"8\n",
"\n",
"2\n",
"3\n",
"5\n",
"7\n",
"11\n",
"2\n",
"3\n",
"5\n",
"7\n",
"11\n",
"2\n",
"4\n",
"6\n",
"8\n"
]
}
],
"source": [
"class MyIterator:\n",
" def __init__(self, data):\n",
" # The data to iterate over\n",
" self.data = data\n",
"\n",
" # Initialize the starting index\n",
" self.index = 0\n",
"\n",
" def __iter__(self):\n",
" # Return the iterator object itself\n",
" return self\n",
"\n",
" def __next__(self):\n",
" # Check if there are more items\n",
" if self.index < len(self.data):\n",
" # Get the current item\n",
" result = self.data[self.index]\n",
"\n",
" # Move to the next index\n",
" self.index += 1\n",
"\n",
" # Return the current item\n",
" return result\n",
" else:\n",
" # No more items, end the iteration\n",
" raise StopIteration\n",
"\n",
"# Usage of MyIterator\n",
"numbers_list = [1, 2, 3, 4, 5] # A simple list\n",
"iterator = MyIterator(numbers_list) # Create an iterator for the list\n",
"\n",
"# Loop through the items using the iterator\n",
"for item in iterator:\n",
" print(item) # Print each item\n",
"\n",
"def my_generator(data):\n",
" # Loop through each item in the data\n",
" for item in data:\n",
" # Yield (return) the current item and pause\n",
" yield item\n",
"\n",
"# Usage of my_generator\n",
"numbers_list = [1, 2, 3, 4, 5] # A simple list\n",
"generator = my_generator(numbers_list) # Create a generator\n",
"\n",
"# Loop through the items produced by the generator\n",
"for item in generator:\n",
" print(item) # Print each item\n",
"\n",
"def read_file_lines(filename):\n",
" # Open the file\n",
" with open(filename) as file:\n",
" # Loop through each line in the file\n",
" for line in file:\n",
" # Yield each line without whitespace\n",
" yield line.strip()\n",
"'''\n",
"# Usage of read_file_lines\n",
"for line in read_file_lines('large_file.txt'): # Replace with your file name\n",
" print(line) # Print each line\n",
"\n",
"def count_up_to(n):\n",
" # Start counting from 1\n",
" count = 1\n",
"\n",
" # Count up to n\n",
" while count <= n:\n",
" # Yield the current count\n",
" yield count\n",
"\n",
" # Move to the next number\n",
" count += 1\n",
"\n",
"# Usage of count_up_to\n",
"for number in count_up_to(5): # Change the number to whatever you want\n",
" print(number) # Print each number\n",
"'''\n",
"def filter_even(numbers):\n",
" # Loop through each number\n",
" for number in numbers:\n",
" # Check if the number is even\n",
" if number % 2 == 0:\n",
" # Yield the even number\n",
" yield number\n",
"\n",
"def square(numbers):\n",
" # Loop through each number\n",
" for number in numbers:\n",
" # Yield the square of the number\n",
" yield number * number\n",
"\n",
"# Usage of filter_even and square\n",
"numbers = range(10) # Create a range of numbers from 0 to 9\n",
"even_numbers = filter_even(numbers) # Get only even numbers\n",
"squared_evens = square(even_numbers) # Square those even numbers\n",
"\n",
"# Loop through the squared results\n",
"for result in squared_evens:\n",
" print(result) # Print each squared even number\n",
"\n",
"numbers = [1, 3, 5, 7]\n",
"\n",
"# find all the methods inside the numbers list\n",
"print(dir(numbers))\n",
"\n",
"numbers = [1, 3, 5, 7]\n",
"\n",
"# call the __iter__() method of the list\n",
"iter_value = numbers.__iter__()\n",
"print(iter_value)\n",
"numbers = [2, 4, 6, 8]\n",
"\n",
"# get the iterator\n",
"iter_value = numbers.__iter__()\n",
"\n",
"# call the next method\n",
"item1 = iter_value.__next__()\n",
"print(item1)\n",
"\n",
"numbers = [2, 4, 6, 8]\n",
"\n",
"# get the iterator\n",
"iter_value = numbers.__iter__()\n",
"\n",
"# call the next method\n",
"item1 = iter_value.__next__()\n",
"print(item1)\n",
"\n",
"# access the next item\n",
"item2 = iter_value.__next__()\n",
"print(item2)\n",
"\n",
"# access the next item\n",
"item3 = iter_value.__next__()\n",
"print(item3)\n",
"\n",
"# access the next item\n",
"item4 = iter_value.__next__()\n",
"print(item4)\n",
"\n",
"numbers = [2, 4, 6, 8]\n",
"\n",
"# get the iterator\n",
"iter_value = iter(numbers)\n",
"\n",
"# call the next method\n",
"item1 = next(iter_value)\n",
"print(item1)\n",
"\n",
"# access the next item\n",
"item2 = next(iter_value)\n",
"print(item2)\n",
"\n",
"# access the next item\n",
"item3 = next(iter_value)\n",
"print(item3)\n",
"\n",
"# access the next item\n",
"item4 = next(iter_value)\n",
"print(item4)\n",
"\n",
"numbers = [2, 4, 6, 8]\n",
"\n",
"# get the iterator\n",
"iter_value = iter(numbers)\n",
"\n",
"# call the next method\n",
"item1 = next(iter_value)\n",
"print(item1)\n",
"\n",
"# access the next item\n",
"item2 = next(iter_value)\n",
"print(item2)\n",
"\n",
"# access the next item\n",
"item3 = next(iter_value)\n",
"print(item3)\n",
"\n",
"# access the next item\n",
"item4 = next(iter_value)\n",
"print(item4)\n",
"\n",
"# access the next item\n",
"item5 = (iter_value)\n",
"print(item5)\n",
"\n",
"num_list = [2, 3, 5, 7, 11]\n",
"\n",
"# for loop to iterate through list\n",
"for element in num_list:\n",
" print(element)\n",
"\n",
"um_list = [2, 3, 5, 7, 11]\n",
"\n",
"# create an iterator object\n",
"iter_obj = iter(num_list)\n",
"\n",
"# loop is always true\n",
"while True:\n",
" try:\n",
" # access each element of list using next()\n",
" element = next(iter_obj)\n",
" print(element)\n",
"\n",
" # if the next() method reaches the end\n",
" # it will throw the exception\n",
" except StopIteration:\n",
" break\n",
"\n",
"class Even:\n",
" def __init__(self, max):\n",
" self.n = 2\n",
" self.max = max\n",
"\n",
" def __iter__(self):\n",
" return self\n",
"\n",
" # customize the next() method to return only even numbers\n",
" def __next__(self):\n",
" if self.n <= self.max:\n",
" result = self.n\n",
" self.n += 2\n",
" return result\n",
"\n",
" else:\n",
" raise StopIteration\n",
"\n",
"\n",
"numbers = Even(10)\n",
"\n",
"print(next(numbers))\n",
"print(next(numbers))\n",
"print(next(numbers))\n",
"print(next(numbers))\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"source": [
"# **Iterators in Python**\n",
"An iterator is an object that allows you to traverse (or loop through) a collection (like a list) one item at a time. You can think of it like reading a book, where you turn one page at a time.\n",
"\n",
"2.1. The Iterator Protocol\n",
"Iterators follow specific rules known as the iterator protocol. This consists of two main methods:\n",
"\n",
"__iter__(): This method initializes the iterator and returns it.\n",
"__next__(): This method returns the next item in the collection. If there are no more items, it raises a StopIteration error to signal that the iteration is complete."
],
"metadata": {
"id": "ytV1aEsvSRmR"
}
},
{
"cell_type": "code",
"source": [
"fruits = [\"apple\", \"mango\", \"cherry\"]\n",
"it = iter(fruits)\n",
"\n",
"print(next(it)) # apple\n",
"print(next(it)) # mango\n",
"print(next(it)) # cherry\n",
"\n",
"my_list = [10, 20, 30]\n",
"myiterator = iter(my_list)\n",
"print(type(myiterator)) # Checking the type of the iterator\n",
"\n",
"my_list = [10, 20, 30]\n",
"myiterator = iter(my_list)\n",
"\n",
"print(next(myiterator)) # 10\n",
"print(next(myiterator)) # 20\n",
"print(next(myiterator)) # 30\n",
"# print(next(myiterator)) # Uncommenting this line raises StopIteration\n",
"\n",
"# Define an iterable (a list)\n",
"numbers = [1, 2, 3]\n",
"\n",
"# Get an iterator from the iterable\n",
"iterator = iter(numbers)\n",
"\n",
"# Use next() to access items one by one\n",
"print(next(iterator)) # Output: 1\n",
"print(next(iterator)) # Output: 2\n",
"print(next(iterator)) # Output: 3\n",
"\n",
"# print(next(iterator)) # Uncommenting this will raise StopIteration\n",
"\n",
"# Define an iterable (a list)\n",
"numbers = [1, 2, 3]\n",
"\n",
"# Create an iterator using iter()\n",
"iterator = iter(numbers)\n",
"\n",
"# Access items using next()\n",
"print(next(iterator))\n",
"print(next(iterator))\n",
"print(next(iterator))\n",
"\n",
"# Define a list\n",
"numbers = [1, 2, 3]\n",
"\n",
"# Use a for loop to iterate\n",
"for num in numbers:\n",
" print(num)\n",
"\n",
"class CountUpto:\n",
" def __init__(self, limit):\n",
" self.limit = limit\n",
" self.current = 1\n",
"\n",
" def __iter__(self):\n",
" return self # The class itself is the iterator\n",
"\n",
" def __next__(self):\n",
" if self.current <= self.limit:\n",
" value = self.current\n",
" self.current += 1\n",
" return value\n",
" else:\n",
" raise StopIteration\n",
"\n",
"# Create an iterator object\n",
"counter = CountUpto(3)\n",
"\n",
"# Iterate using next()\n",
"print(next(counter)) # Output: 1\n",
"print(next(counter)) # Output: 2\n",
"print(next(counter)) # Output: 3\n",
"# print(next(counter)) # Raises StopIteration\n",
"\n",
"\n",
"class Counter:\n",
" def __init__(self, max):\n",
" self.num = 1\n",
" self.max = max\n",
"\n",
" def __iter__(self):\n",
" return self\n",
"\n",
" def __next__(self):\n",
" if self.num <= self.max:\n",
" val = self.num\n",
" self.num += 1\n",
" return val\n",
" else:\n",
" raise StopIteration\n",
"\n",
"# Create object\n",
"counter = Counter(3)\n",
"\n",
"for num in counter:\n",
" print(num)\n",
"\n",
"import itertools\n",
"\n",
"# Create an infinite iterator starting from 1\n",
"counter = itertools.count(1)\n",
"\n",
"for num in counter:\n",
" print(num)\n",
" if num == 5:\n",
" break\n",
"\n",
"\n",
"x = [1, 2, 3, 4, 5]\n",
"\n",
"for i in x: # funcion iter is called by default\n",
" print(i)\n",
"\n",
"for i in iter(x):\n",
" print(i)\n",
"\n",
"\n",
"x = [1, 2, 3, 4, 5]\n",
"\n",
"it = iter(x)\n",
"\n",
"for i in range(5):\n",
" print(next(it))\n",
"\n",
"class NumberFromSequence:\n",
" # we get numbers daily in a string\n",
" def __init__(self, daily_results: str) -> None:\n",
" self.daily_results = daily_results\n",
" self.results_separated = daily_results.split(' ')\n",
"\n",
" def __iter__(self):\n",
" return NumbersIterator(self.results_separated)\n",
"\n",
"class NumbersIterator:\n",
" def __init__(self, numbers) -> None:\n",
" self.numbers = numbers\n",
" self.index = 0\n",
"\n",
" def __next__(self):\n",
" try:\n",
" number = self.numbers[self.index]\n",
" except IndexError:\n",
" raise StopIteration()\n",
" self.index += 1\n",
" return number\n",
"\n",
" def __iter__(self):\n",
" return self\n",
"\n",
"nums = NumberFromSequence('20 31 21 54 90')\n",
"for i in nums: # we call class instance, not the list nums.results_separated\n",
" print(i)\n",
"\n",
"# 20\n",
"# 31\n",
"# 21\n",
"# 54\n",
"# 90\n",
"\n",
"class NumberFromSequence:\n",
" # we get numbers daily in a string\n",
" def __init__(self, daily_results: str) -> None:\n",
" self.daily_results = daily_results\n",
"\n",
" def __iter__(self):\n",
" # make it more lazy - not storing list of numbers to attribute\n",
" for number in self.daily_results.split(' '):\n",
" yield number # no need to explicit return\n",
"\n",
"nums = NumberFromSequence('20 31 21 54 90')\n",
"for i in nums:\n",
" print(i)\n",
"\n",
"import sys\n",
"\n",
"def gen_list(numbers):\n",
" yield from numbers\n",
"\n",
"# small case\n",
"numbers = list(range(10))\n",
"gen_1 = gen_list(numbers)\n",
"size_1 = sys.getsizeof(gen_1)\n",
"\n",
"# more numbers in list\n",
"numbers = list(range(10000000))\n",
"gen_2 = gen_list(numbers)\n",
"size_2 = sys.getsizeof(gen_2)\n",
"\n",
"print(size_1, size_2)\n",
"# 192 192\n",
"\n",
"from itertools import count\n",
"# create a generator\n",
"gen = count(100, 10)\n",
"\n",
"# we can create an infititive progression if needed with while loop\n",
"for i in range(5):\n",
" print(next(gen))\n",
"\n"
],
"metadata": {
"id": "if2-wchMSRuB",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8f623eb7-0ffc-444c-9fc3-ea6a2827010d"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"apple\n",
"mango\n",
"cherry\n",
"\n",
"10\n",
"20\n",
"30\n",
"1\n",
"2\n",
"3\n",
"1\n",
"2\n",
"3\n",
"1\n",
"2\n",
"3\n",
"1\n",
"2\n",
"3\n",
"1\n",
"2\n",
"3\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"20\n",
"31\n",
"21\n",
"54\n",
"90\n",
"20\n",
"31\n",
"21\n",
"54\n",
"90\n",
"192 192\n",
"100\n",
"110\n",
"120\n",
"130\n",
"140\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Generators in Python**\n",
"A generator is a simpler way to create an iterator using a function that yields values. The key feature of a generator is the yield statement, which allows you to return a value and pause the function’s execution.\n",
"\n",
"Working of Generators in Python\n",
"When you call a generator function, it doesn’t execute the entire function at once. Instead, it runs until it hits a yield statement, at which point it pauses and saves its state. The next time you call it, it resumes from where it left off."
],
"metadata": {
"id": "6J9ZOc53SR1p"
}
},
{
"cell_type": "code",
"source": [
"from itertools import batched\n",
"\n",
"data = list(range(0, 30, 3)) # creates list of 10 items\n",
"gen = batched(data, 4) # build a generator\n",
"\n",
"for i in gen:\n",
" print(i)\n",
"# (0, 3, 6, 9)\n",
"# (12, 15, 18, 21)\n",
"# (24, 27)\n",
"# last output has only 2 items - all that remained in starting list\n",
"\n",
"class NumberFromSequence:\n",
" # we get numbers daily in a string\n",
" def __init__(self, daily_results: str) -> None:\n",
" self.daily_results = daily_results\n",
" self.results_separated = daily_results.split(' ')\n",
"\n",
" def __iter__(self):\n",
" # not it's written in one line\n",
" yield from self.results_separated\n",
"\n",
"nums = NumberFromSequence('20 31 21 54 90')\n",
"for i in nums:\n",
" print(i)\n",
"\n",
"\n",
"from collections.abc import Iterator\n",
"\n",
"def fibonacci() -> Iterator:\n",
" a, b = 0, 1\n",
" while True:\n",
" yield a\n",
" a, b = b, a + b\n",
"\n",
"mylist = [13, 46, -3, 'Go!']\n",
"myiter = iter(mylist) # get the list iterator\n",
"\n",
"try:\n",
" while True:\n",
" val = next(myiter)\n",
" print(val, end=' ')\n",
"except StopIteration:\n",
" print('Stop!')\n",
"\n",
"some_list = [1, 2]\n",
"iterator_of_some_list = iter(some_list)\n",
"for i in iterator_of_some_list:\n",
" print(i)\n",
"for j in iterator_of_some_list: # doesnt work\n",
" print(j)\n",
"\n",
"# but\n",
"for k in some_list:\n",
" print(k)\n",
"for l in some_list: # works\n",
" print(l)\n",
"\n",
"some_list = [1, 2]\n",
"\n",
"for k in some_list:\n",
" for l in some_list:\n",
" print(l, k)\n",
"\n",
"colors = ['Black', 'Purple', 'Green']\n",
"iterator = iter(colors)\n",
"print(iterator)\n",
"\n",
"colors = ['Black', 'Purple', 'Green']\n",
"iterator = iter(colors)\n",
"print(next(iterator)) # Output: Black\n",
"print(next(iterator)) # Output: Purple\n",
"print(next(iterator)) # Output: Green\n",
"\n",
"\n",
"MyList = ['MON', 'TUE', 'WED']\n",
"MyIter = iter(MyList)\n",
"\n",
"print(next(MyIter))\n",
"print(next(MyIter))\n",
"print(next(MyIter))\n",
"\n",
"MyList = ['MON', 'TUE', 'WED']\n",
"\n",
"for i in MyList:\n",
" print(i)\n",
"\n",
"class MyIterClass:\n",
" def __iter__(self):\n",
" self.a = 1\n",
" return self\n",
"\n",
" def __next__(self):\n",
" x = self.a\n",
" self.a += 1\n",
" y = x * x\n",
" return y\n",
"\n",
"IterObj = MyIterClass()\n",
"MyIter = iter(IterObj)\n",
"\n",
"for i in range(1, 11):\n",
" print(next(MyIter), end=\" \")\n",
"\n",
"class MyIterClass:\n",
" def __iter__(self):\n",
" self.a = 1\n",
" return self\n",
"\n",
" def __next__(self):\n",
" if self.a < 6:\n",
" x = self.a\n",
" self.a += 1\n",
" y = x * x\n",
" return y\n",
" else:\n",
" raise StopIteration\n",
"'''\n",
"IterObj = MyIterClass()\n",
"MyIter = iter(IterObj)\n",
"\n",
"for i in range(1, 11):\n",
" print(next(MyIter))\n",
"'''\n",
"a = [0, 5, 10, 15, 20]\n",
"for i in a:\n",
" if i % 2 == 0:\n",
" print(str(i)+' is an Even Number')\n",
" else:\n",
" print(str(i)+' is an Odd Number')\n",
"\n",
"iter_list = iter(['Geeks', 'For', 'Geeks'])\n",
"print(next(iter_list))\n",
"print(next(iter_list))\n",
"print(next(iter_list))\n",
"\n",
"def sq_numbers(n):\n",
" for i in range(1, n+1):\n",
" yield i*i\n",
"\n",
"\n",
"a = sq_numbers(3)\n",
"\n",
"print(\"The square of numbers 1,2,3 are : \")\n",
"print(next(a))\n",
"print(next(a))\n",
"print(next(a))\n",
"\n",
"class TenIntegers:\n",
"\n",
" def __init__(self, start_from=0):\n",
" self.current=start_from\n",
" self.max = self.current + 10\n",
"\n",
" def __iter__(self):\n",
" '''\n",
" This method makes the object iterable\n",
" '''\n",
" return self\n",
"\n",
" def __next__(self):\n",
" '''\n",
" This is the actual method used on iteration\n",
" '''\n",
" if self.current < self.max:\n",
" current_value = self.current\n",
" self.current += 1\n",
" return current_value\n",
" else:\n",
" # This error is raised to stop the iteration\n",
" raise StopIteration\n",
"\n",
" # Useful to call it manually\n",
" next = __next__\n",
"\n",
"for a in TenIntegers():\n",
" print(a)\n",
"\n",
"manual_iterator = TenIntegers(100)\n",
"for _ in range(0, 10):\n",
" print(manual_iterator.next())\n",
"'''\n",
"# iterating 11 times, it raises an error\n",
"for _ in range(0, 11):\n",
" print(manual_iterator.next())\n",
"'''\n",
"for a in TenIntegers():\n",
" print(a)\n",
"\n",
"manual_iterator = TenIntegers(100)\n",
"for _ in range(0, 10):\n",
" print(manual_iterator.__next__())\n",
"\n"
],
"metadata": {
"id": "O6u4SGflSR-5",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0da83673-0361-44a4-b15b-4bd77d763a72"
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"(0, 3, 6, 9)\n",
"(12, 15, 18, 21)\n",
"(24, 27)\n",
"20\n",
"31\n",
"21\n",
"54\n",
"90\n",
"13 46 -3 Go! Stop!\n",
"1\n",
"2\n",
"1\n",
"2\n",
"1\n",
"2\n",
"1 1\n",
"2 1\n",
"1 2\n",
"2 2\n",
"\n",
"Black\n",
"Purple\n",
"Green\n",
"MON\n",
"TUE\n",
"WED\n",
"MON\n",
"TUE\n",
"WED\n",
"1 4 9 16 25 36 49 64 81 100 0 is an Even Number\n",
"5 is an Odd Number\n",
"10 is an Even Number\n",
"15 is an Odd Number\n",
"20 is an Even Number\n",
"Geeks\n",
"For\n",
"Geeks\n",
"The square of numbers 1,2,3 are : \n",
"1\n",
"4\n",
"9\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"100\n",
"101\n",
"102\n",
"103\n",
"104\n",
"105\n",
"106\n",
"107\n",
"108\n",
"109\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"100\n",
"101\n",
"102\n",
"103\n",
"104\n",
"105\n",
"106\n",
"107\n",
"108\n",
"109\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **What Does yield Do?**\n",
"Pauses the Function: When the function hits yield, it stops and returns the value.\n",
"Saves State: It keeps track of where it left off, including variable values and execution point.\n",
"Resumes Later: The next time you call next() on the generator, it picks up right after the last yield."
],
"metadata": {
"id": "I0jXR3fVSSHh"
}
}
]
}