1 00:00:00,420 --> 00:00:03,120 We are going to discuss Biton iterators. 2 00:00:04,280 --> 00:00:11,870 So iterators are present in Baton and they are mostly implemented within four loop's comprehensions, 3 00:00:12,080 --> 00:00:16,210 generators, etc., but hidden in plain sight. 4 00:00:16,910 --> 00:00:23,780 So a delegate in Python is simply an object that can iterated upon an object which will return. 5 00:00:23,780 --> 00:00:26,220 Data are the one element at the time. 6 00:00:26,720 --> 00:00:34,250 So this iterator will be operating on a sequence of elements and each and every iteration it will pick 7 00:00:34,250 --> 00:00:35,660 up one element at a time. 8 00:00:36,140 --> 00:00:43,010 So it's written in Python is simply an object that can be iterated upon an object which is which will 9 00:00:43,010 --> 00:00:45,920 return data one element at a time. 10 00:00:46,460 --> 00:00:51,730 Technically speaking, Biton iterator object must implement two special methods. 11 00:00:51,830 --> 00:00:57,030 So what are the two special methods that is understood, understood, etre, underscore, underscore, 12 00:00:57,050 --> 00:01:01,520 method and underscore, underscore, next, underscore, underscore method. 13 00:01:01,760 --> 00:01:07,390 So it's the method and the next method collectively called the iterator protocol. 14 00:01:07,610 --> 00:01:11,360 So it's a method that is to retrieve their respective value. 15 00:01:11,360 --> 00:01:15,610 At the next, he will check whether the next element is there in the sequence or not. 16 00:01:17,660 --> 00:01:25,310 So an object is called iterable, if we can get an iterator from it, and most of the built in containers 17 00:01:25,310 --> 00:01:33,090 in Biton, like the least double strings, etc, are iterable because we can extract we can get iterator 18 00:01:33,110 --> 00:01:40,130 from it, that it did a function which in turn call that etre that is understood, understood, itr 19 00:01:40,130 --> 00:01:44,810 underscore, underscore method returns and iterator from them. 20 00:01:45,280 --> 00:01:52,130 The next that is underscore, underscore, next underscore, underscore method must return the next 21 00:01:52,130 --> 00:02:00,850 item in the sequence on the reaching at the end and in the subsequent calls it must raise stop iteration. 22 00:02:01,250 --> 00:02:07,190 So whenever we are having no item in the in the iterator remaining. 23 00:02:07,340 --> 00:02:12,730 But you are calling this underscore, underscore next underscore the underscore method, then one one 24 00:02:12,740 --> 00:02:15,660 exception will be raised and that is a stop iteration. 25 00:02:16,220 --> 00:02:23,090 So let us go for one practical demonstration to show you that how this python iterators can easily be 26 00:02:23,090 --> 00:02:25,220 implemented in python coding. 27 00:02:25,400 --> 00:02:27,890 So here is the demonstration for you. 28 00:02:28,730 --> 00:02:35,780 Iterators are very common in our python programming language and these are elegantly implemented within 29 00:02:35,780 --> 00:02:43,790 our four loops, generators, comprehensions, etc. but hidden in plain sight and iterated in Python 30 00:02:43,790 --> 00:02:45,470 is nothing but an object. 31 00:02:45,620 --> 00:02:50,570 And this object will return detA one at a time during our iteration. 32 00:02:50,670 --> 00:02:56,720 Obviously, if some data is available, then it will return the data, otherwise it will throw some 33 00:02:56,720 --> 00:02:57,280 exception. 34 00:02:57,800 --> 00:03:02,150 So technically speaking, Python Étretat object must implement two special methods. 35 00:03:02,160 --> 00:03:03,500 One is the next method. 36 00:03:03,800 --> 00:03:07,670 So Kano's we call does underscore, underscore, next underscore the underscore method. 37 00:03:07,880 --> 00:03:13,340 And out of the one is the ITR method that is actually underscored, underscored, etre underscore, 38 00:03:13,340 --> 00:03:20,240 underscore the method and collectively they will be known as the iterator protocol and object is called 39 00:03:20,240 --> 00:03:24,380 iterable if we can get an iterator from it. 40 00:03:24,830 --> 00:03:31,910 Most of the building content in our python, like our string, like our top, at least having they are 41 00:03:31,910 --> 00:03:39,740 nothing but eatables that entire method or the ITR function, which in turn called the itr underscore, 42 00:03:39,740 --> 00:03:45,130 underscore etre underscored underscore method returns and iterator from them. 43 00:03:45,530 --> 00:03:51,180 So let us go through this example and to discuss the property of this iterator. 44 00:03:51,590 --> 00:03:58,310 So here you see here to define one list here and this item start from using this method from this it 45 00:03:58,490 --> 00:04:06,020 from this list we are getting one iterated and to access that data from this iterator, we are using 46 00:04:06,020 --> 00:04:07,490 the method next here. 47 00:04:07,820 --> 00:04:10,040 So either we can use this next already. 48 00:04:10,520 --> 00:04:14,720 So do the syntax to retrieve elements from this iterator. 49 00:04:14,720 --> 00:04:16,490 That is our return address. 50 00:04:16,490 --> 00:04:21,250 We can write our return dot, underscore, underscore, next underscore, underscore method. 51 00:04:21,270 --> 00:04:23,110 So anyone we can use that one. 52 00:04:23,510 --> 00:04:28,390 So here to see the list is containing data that is forty four, seven, seven, 11 and 33. 53 00:04:28,730 --> 00:04:36,710 So for the next unretiring 44 next mean there is seventy seven next element which will be retrieved 54 00:04:36,710 --> 00:04:40,660 here is eleven next eleven which will be added to here 33. 55 00:04:41,000 --> 00:04:44,550 So now we have got all the four elements now. 56 00:04:44,610 --> 00:04:48,620 Now if we go for next hour, later it will raise one exception. 57 00:04:48,890 --> 00:04:51,740 So let let us go for the execution of this block. 58 00:04:52,820 --> 00:04:59,930 So here we are having forty four, seventy seven, 11 and 33 because the elements were 44, 77, 11 59 00:04:59,930 --> 00:05:00,740 and 33. 60 00:05:01,040 --> 00:05:09,650 So when we execute that print next hour, greater than 44 was retrieved and next print next hour after 61 00:05:09,670 --> 00:05:14,810 we execute this method, then 77 has been retrieved from this iterator. 62 00:05:15,050 --> 00:05:16,850 That is our return next time. 63 00:05:16,860 --> 00:05:17,830 Eleven next time. 64 00:05:17,840 --> 00:05:18,500 Thirty three. 65 00:05:18,500 --> 00:05:22,850 Now, if I execute this one, I should not put this one in comment. 66 00:05:22,850 --> 00:05:28,880 If I execute this one, then one exception will be there and there is a stop iteration and this will 67 00:05:28,880 --> 00:05:32,390 raise no items left, as we mentioned in the comment. 68 00:05:32,600 --> 00:05:36,050 And so next hour it will produce this error. 69 00:05:36,050 --> 00:05:37,520 That is a stop iteration. 70 00:05:38,120 --> 00:05:39,590 So let us go for another one. 71 00:05:40,370 --> 00:05:42,220 So create a custom iterator. 72 00:05:42,590 --> 00:05:45,980 So here we are defining one class that is the power of two. 73 00:05:46,310 --> 00:05:49,760 And here we are having this document documentation string. 74 00:05:49,980 --> 00:05:52,820 In short, we can call it as dark string. 75 00:05:53,120 --> 00:05:58,970 So it should be enclosed either within a single class or triple double quotes. 76 00:05:59,270 --> 00:06:05,330 So it is also known as a block comment, and it should be the first intended, intended statement in 77 00:06:05,330 --> 00:06:06,110 our class. 78 00:06:06,470 --> 00:06:11,420 So next, we are having this constructor that is our underscore, underscore in it, underscore, underscore. 79 00:06:11,750 --> 00:06:15,070 So here you are defining this, Max, and this is a goal. 80 00:06:15,080 --> 00:06:16,850 Digital is the default argument. 81 00:06:17,410 --> 00:06:22,690 So whatever the value will be passing that will be assigned to this Max instance variable, so here 82 00:06:22,690 --> 00:06:28,030 we are just defining this underscore, underscore, Peter, underscore, underscore method and similarly 83 00:06:28,240 --> 00:06:31,520 this underscore, underscore, next underscore, underscore method. 84 00:06:31,750 --> 00:06:36,380 So these two methods we are defining here, so self-taught in physical digital. 85 00:06:36,460 --> 00:06:42,230 So in another instance, rideable is getting initialized with the Vallecito and then having this next. 86 00:06:42,250 --> 00:06:49,180 So if self got in is less than equal to Max, then the Israelis are due to start start. 87 00:06:49,480 --> 00:06:55,690 That is due to the power of self-taught ends up in will be assigned to the result and self-taught and 88 00:06:55,690 --> 00:06:58,440 will be increased by one and the result will be returned. 89 00:06:58,450 --> 00:07:04,870 Otherwise that else in the part we are going for stop iteration where it is this error, there is a 90 00:07:04,870 --> 00:07:05,890 stop iteration. 91 00:07:06,580 --> 00:07:10,260 So now here at first we are going to print this dark string. 92 00:07:10,270 --> 00:07:16,600 There is a documentation string and then triple single codes can also be included within the triple 93 00:07:16,600 --> 00:07:17,370 double codes. 94 00:07:17,590 --> 00:07:24,100 So whenever we are printing this, this one, there is a part of two that is a class named Underscore, 95 00:07:24,100 --> 00:07:26,030 underscore, underscore, underscore. 96 00:07:26,050 --> 00:07:28,060 You can see that it is printing. 97 00:07:28,900 --> 00:07:30,030 Let us go for this. 98 00:07:30,040 --> 00:07:35,560 It is printing this respective string that is a class to implement and iterator of powers of two. 99 00:07:35,800 --> 00:07:38,940 So that string whatever is discussed or defined here. 100 00:07:39,370 --> 00:07:40,490 So it has got printed. 101 00:07:40,930 --> 00:07:42,310 So now we are going for this. 102 00:07:42,730 --> 00:07:44,780 It is equal to power of two four. 103 00:07:44,800 --> 00:07:47,340 So now this fall will be assigned to Max. 104 00:07:47,650 --> 00:07:49,000 This will be assigned to Max. 105 00:07:49,000 --> 00:07:50,660 So Max has got the value for here. 106 00:07:51,100 --> 00:07:55,180 So now we are going for this bicycle to iterate. 107 00:07:55,390 --> 00:07:57,400 So we are going for this next day. 108 00:07:57,440 --> 00:07:57,970 Next day. 109 00:07:57,970 --> 00:07:58,390 The next day. 110 00:07:58,390 --> 00:07:59,920 It is going on going on printing. 111 00:08:00,140 --> 00:08:04,000 That is to the part of zero because Ania's having the initial value zero here. 112 00:08:04,420 --> 00:08:08,890 So to zero the result will become one and return result. 113 00:08:08,900 --> 00:08:10,940 So that result will be printed as one. 114 00:08:11,350 --> 00:08:17,560 So next time this end has got increased by one or two to one, the result will have the value to and 115 00:08:17,560 --> 00:08:18,340 return the result. 116 00:08:18,340 --> 00:08:20,180 That means it will return the value to here. 117 00:08:20,200 --> 00:08:25,290 So too is getting printed because we have called this next day from this print method. 118 00:08:25,540 --> 00:08:30,830 So now next time the value of N will become too so to become four. 119 00:08:30,850 --> 00:08:35,170 So this will have the value for and the result will be returned and that will get printed. 120 00:08:35,200 --> 00:08:40,000 So in this way, I'm getting this one as one to four as output. 121 00:08:40,420 --> 00:08:45,490 So I think you are getting this idea how this method, which is having this in it, there is a constructor, 122 00:08:45,490 --> 00:08:49,270 this ITR method, the next method and the respective object. 123 00:08:49,270 --> 00:08:51,010 We are defined under the class. 124 00:08:51,010 --> 00:08:52,060 That is our aim. 125 00:08:52,510 --> 00:08:58,420 So this Max has got initialized with the value for which has been passed as a parameter to this constructor. 126 00:08:58,780 --> 00:09:01,210 Then out of this eight, we are creating one item. 127 00:09:01,420 --> 00:09:06,880 So this iterator is this one I and from Iterator we can pick up the next Palouse. 128 00:09:07,060 --> 00:09:08,410 So there is a next of I. 129 00:09:08,410 --> 00:09:10,090 So this method is getting called. 130 00:09:10,510 --> 00:09:15,760 So we can also call it, as I underscore, underscore, next underscore, underscore method. 131 00:09:15,790 --> 00:09:17,170 So let me bring it here. 132 00:09:17,170 --> 00:09:23,590 So I dot underscored, underscore, next underscore, underscore methods to also we can print it. 133 00:09:24,430 --> 00:09:27,720 So now let me, let me go for the execution of the same. 134 00:09:27,730 --> 00:09:29,530 So I'm just erasing this part. 135 00:09:29,710 --> 00:09:30,790 It is not required. 136 00:09:34,630 --> 00:09:40,660 OK, so now if we execute the same I'm getting the same output because next day is actually executing 137 00:09:40,660 --> 00:09:43,920 IDOT underscored, underscore, next underscore, underscore method. 138 00:09:44,500 --> 00:09:49,310 So next week we are going for creating a infinite custom iterator. 139 00:09:49,660 --> 00:09:51,730 So in this case, this is our Doch string. 140 00:09:51,730 --> 00:09:54,020 The name of the class is Infinite Etre. 141 00:09:54,400 --> 00:09:58,680 And here we are having this method literally of define the next we have defined. 142 00:09:59,080 --> 00:10:01,190 So this is the infinite eater object. 143 00:10:01,190 --> 00:10:03,310 The class object is I from the site. 144 00:10:03,310 --> 00:10:08,810 We're creating one iterator using the method etre and then we are going for this next E! 145 00:10:09,190 --> 00:10:11,740 So here you see the NUM has got the value one. 146 00:10:12,010 --> 00:10:16,000 So NUM is equal to num num num has got the value one here. 147 00:10:16,120 --> 00:10:18,400 Sultanahmet is equal to two. 148 00:10:18,670 --> 00:10:24,070 So in this way it will go on creating because there is no logic is there, to stop this one. 149 00:10:24,070 --> 00:10:30,130 It will go on creating, just incrementing this value of NO2 each and every time and then returning 150 00:10:30,130 --> 00:10:32,830 that number is a previous value of NUM will be returned. 151 00:10:33,220 --> 00:10:37,930 So now we are having this next day, the next day, next in this way, if you go on all in the next 152 00:10:37,930 --> 00:10:38,430 method. 153 00:10:38,620 --> 00:10:41,100 So at first the number was having the value one. 154 00:10:41,410 --> 00:10:44,030 So this one will be assigned to NUM, which will be returned. 155 00:10:44,050 --> 00:10:45,470 So that is getting printed. 156 00:10:45,700 --> 00:10:48,730 So one will get printed, then three, then five and seven. 157 00:10:48,970 --> 00:10:51,370 In this way the values will go on printing. 158 00:10:51,370 --> 00:10:53,320 So it has got no dominating logic. 159 00:10:53,560 --> 00:10:57,830 So that's why it is infinite etre, the name of the class has been given accordingly. 160 00:10:58,270 --> 00:11:04,210 So from this demonstration, you are getting this idea that how this iterator can play its role in our 161 00:11:04,210 --> 00:11:05,090 Python programming. 162 00:11:05,650 --> 00:11:06,880 Thanks for watching this video.