1 00:00:00,560 --> 00:00:03,930 In this situation, we're discussing python generators. 2 00:00:06,210 --> 00:00:13,400 So iterators in Biton incurred loss of overrates, implementing a class with underscore, underscored, 3 00:00:13,410 --> 00:00:18,530 itr, underscore, underscore method and the underscore, underscore, next underscore, underscore 4 00:00:18,550 --> 00:00:27,360 method and then keeping track of internal stiff's race stop iteration when there was no values to be 5 00:00:27,360 --> 00:00:28,650 returned, etc.. 6 00:00:28,650 --> 00:00:34,500 So there's too much overhead in Kessab iterator so that things will be minimized, that overhead will 7 00:00:34,500 --> 00:00:36,670 get minimized in our generators. 8 00:00:37,050 --> 00:00:40,620 So this is both lyndy and counter intuitive. 9 00:00:41,100 --> 00:00:44,970 So generator comes with more simplicities. 10 00:00:45,000 --> 00:00:48,450 So what are the simplicities we're going to enjoy in our generators? 11 00:00:48,900 --> 00:00:55,980 So Biton generators are a simple way of creating iterators and all the overate we mentioned above are 12 00:00:56,010 --> 00:01:00,630 automatically handled by the generators in our Biton coding. 13 00:01:01,140 --> 00:01:07,200 So simply speaking, a generator is a function that returns an object that is the iterator object, 14 00:01:07,440 --> 00:01:11,400 which we can iterate over one value at a time. 15 00:01:11,640 --> 00:01:14,660 So this is the simplest way to handle that. 16 00:01:14,880 --> 00:01:16,740 That is pretty iterator object. 17 00:01:18,730 --> 00:01:22,510 Difference between the generator function and a normal function. 18 00:01:22,570 --> 00:01:26,240 What is the basic difference between a normal function and a generator function? 19 00:01:26,650 --> 00:01:30,310 A generator function contains one or more statement. 20 00:01:31,400 --> 00:01:39,920 When called, it returns an object that is iterator, but does not start execution immediately, methods 21 00:01:39,920 --> 00:01:44,420 like your underscore underscored Peter underscore, underscore, method and underscore, underscore 22 00:01:44,420 --> 00:01:51,770 next underscore the underscore method are implemented automatically in our generators so we can iterate 23 00:01:51,770 --> 00:01:54,250 through the items using next. 24 00:01:54,770 --> 00:01:58,550 So not that very one that is underscored on the next underscore. 25 00:01:58,550 --> 00:02:01,520 On the second method, we can use this next method. 26 00:02:01,880 --> 00:02:09,260 Once the function is, the function is paused and the control is transferred to the caller and local 27 00:02:09,260 --> 00:02:13,930 variables and their states are remembered between successive calls. 28 00:02:14,300 --> 00:02:21,460 And finally, when the function terminates, stop iteration is raised automatically on the father column. 29 00:02:22,040 --> 00:02:28,670 So here to see in case of Iterator, we're having one very special statement that is known as that you 30 00:02:28,790 --> 00:02:34,600 stickman, which will accumulate all these respective values which will be obtained from these iterations. 31 00:02:35,000 --> 00:02:36,980 So I think for the better idea, let us go. 32 00:02:36,980 --> 00:02:42,470 For one example, during the explanation and during the writing of the coding will be going for further 33 00:02:42,470 --> 00:02:44,670 explanation, for better understanding. 34 00:02:44,810 --> 00:02:46,580 So just to watch the demonstration of. 35 00:02:47,440 --> 00:02:53,350 In the previous video where we have discussed this iterator, there is an iterator feature in our Python 36 00:02:53,350 --> 00:02:53,890 programming. 37 00:02:54,110 --> 00:02:59,170 We have seen that we are supposed to deal with Huze of what it was supposed to define the ITR method. 38 00:02:59,170 --> 00:03:01,570 We are supposed to define the next method. 39 00:03:01,780 --> 00:03:04,810 We are supposed to keep the track of the internal states. 40 00:03:04,990 --> 00:03:10,710 There is the stop iteration when there is there are no values to be returned and so many different issues. 41 00:03:10,990 --> 00:03:15,910 And this is our code, more and more lendee and counter intuitive. 42 00:03:16,240 --> 00:03:23,410 And Jeneda comes into to rescue in those situations, python generators are a simple way of creating 43 00:03:23,410 --> 00:03:30,370 iterators and all the overate we mentioned here in our discussion will be automatically handled by the 44 00:03:30,370 --> 00:03:31,840 generators in Python. 45 00:03:31,840 --> 00:03:37,300 And simply speaking, a generator is a function that returns an object that is the iterator object, 46 00:03:37,570 --> 00:03:40,160 which we can iterate over. 47 00:03:40,360 --> 00:03:43,080 That means we can return value one at a time. 48 00:03:43,330 --> 00:03:49,960 So it is fairly simple to create a generator in Python and it is as easy as defining a normal function 49 00:03:50,200 --> 00:03:53,560 with the E Teckman instead of having the return statement. 50 00:03:54,220 --> 00:04:00,340 That difference is that while a return statement dominates, a function internally terminates at that 51 00:04:00,340 --> 00:04:01,030 very statement. 52 00:04:01,330 --> 00:04:03,070 But Inca's of each statement. 53 00:04:03,310 --> 00:04:11,500 Thus the statement pauses the function, saving all its debts and later continues from there on successive 54 00:04:11,500 --> 00:04:12,040 cuts. 55 00:04:12,040 --> 00:04:16,340 And that is the basic difference between the return and the statement. 56 00:04:16,600 --> 00:04:18,910 So let us go for this practical example. 57 00:04:19,000 --> 00:04:20,950 For the better understanding here. 58 00:04:20,950 --> 00:04:24,800 You have defined one method that is a mind generated in Israel. 59 00:04:24,970 --> 00:04:28,010 One, this is printed first ild n. 60 00:04:28,540 --> 00:04:31,570 So this particular state is giving is getting saved here. 61 00:04:31,870 --> 00:04:40,420 So in blast equal to one print, this is printed second ild n and then we're having this in place equal 62 00:04:40,420 --> 00:04:40,840 to one. 63 00:04:40,840 --> 00:04:42,110 So N is getting increased. 64 00:04:42,130 --> 00:04:47,710 Now it has got the value three here because initially it was having the value one and next, next time 65 00:04:47,710 --> 00:04:49,180 it has got increased by one. 66 00:04:49,180 --> 00:04:51,910 So and so now the N has got the value to. 67 00:04:52,390 --> 00:04:55,880 So any Segoline plus one the value up in has become three. 68 00:04:56,170 --> 00:04:59,250 This is printed at last and ild and is there. 69 00:04:59,380 --> 00:05:04,630 So here you see instead of using the return statement to abuse the instrument, not what is the purpose 70 00:05:04,630 --> 00:05:05,050 of this. 71 00:05:05,350 --> 00:05:12,550 So now using this particular method, the return we are sending to this e so e nothing but one iterator 72 00:05:12,550 --> 00:05:13,060 object. 73 00:05:13,330 --> 00:05:20,620 So to have to, to access the elements from this eight at using two is one using one for loop and another 74 00:05:20,630 --> 00:05:22,580 one using the next method. 75 00:05:22,900 --> 00:05:27,020 So next of a so let me go to code so you can get output. 76 00:05:27,220 --> 00:05:28,780 So next up it is printing. 77 00:05:28,810 --> 00:05:30,220 This is the printed first. 78 00:05:30,550 --> 00:05:36,940 Next A it is printing that this is printed sergant next it is printing that this has printed at the 79 00:05:36,940 --> 00:05:38,200 last note. 80 00:05:38,200 --> 00:05:40,080 We are going for this using for loop. 81 00:05:40,390 --> 00:05:42,940 So this particular message has got printed here. 82 00:05:43,390 --> 00:05:45,910 So now iterating using the for loop here. 83 00:05:46,060 --> 00:05:51,340 So for item in, I'm just calling this method that is my underscore generator method. 84 00:05:51,340 --> 00:05:55,060 You are calling and it is returning that their respective iterator. 85 00:05:55,450 --> 00:05:56,760 So print item. 86 00:05:56,770 --> 00:06:00,760 So I'm going for this, I am printing this item here to add having this for each loop. 87 00:06:00,940 --> 00:06:06,580 So each and every time it will retrieve the next element from this iterator which is being returned 88 00:06:06,580 --> 00:06:08,680 by this my generator method. 89 00:06:08,890 --> 00:06:10,180 So it is printing in this way. 90 00:06:10,420 --> 00:06:16,140 This is printed first one, this is their second two and this is printed at last. 91 00:06:16,150 --> 00:06:17,020 That is the battery. 92 00:06:17,440 --> 00:06:23,380 So in this way, you can find that in this generative method I didn't discuss, I didn't find any other 93 00:06:23,380 --> 00:06:24,940 method or next method. 94 00:06:25,270 --> 00:06:29,490 The iterator is getting handled automatically by this generator. 95 00:06:30,610 --> 00:06:36,120 So let us go for another example for the more clarity so generated with the loop. 96 00:06:36,310 --> 00:06:38,200 So here do you find one method? 97 00:06:38,200 --> 00:06:42,340 There is a reverse string which will take this my string as input parameter. 98 00:06:42,670 --> 00:06:47,740 So Lindop that might string will be will be kept in length where we use a method linear. 99 00:06:48,070 --> 00:06:51,360 So for I in range so lenth minus one it is. 100 00:06:51,370 --> 00:06:53,230 Suppose I have given this particular string. 101 00:06:53,230 --> 00:06:53,980 There is a word. 102 00:06:53,990 --> 00:06:56,920 So how many letters are there in this world without having this. 103 00:06:56,920 --> 00:06:57,640 Five letters. 104 00:06:57,940 --> 00:06:59,790 So the length will be five here. 105 00:07:00,040 --> 00:07:00,530 So. 106 00:07:00,530 --> 00:07:02,790 So now minus one means what. 107 00:07:02,830 --> 00:07:03,970 So that is our fault. 108 00:07:04,210 --> 00:07:05,130 Minus one. 109 00:07:05,140 --> 00:07:10,910 We know that whenever we were having this range so it would reach up to zero zero four to zero with 110 00:07:10,910 --> 00:07:11,900 the step minus one. 111 00:07:11,920 --> 00:07:13,380 So this is our step minus one. 112 00:07:13,690 --> 00:07:20,530 So for Basilicata range and because that's why I've given here that the next parameter as our minus 113 00:07:20,530 --> 00:07:22,130 one to this French method. 114 00:07:22,420 --> 00:07:28,960 So now here this Leontes, I mean, of five, so five minus one is for four to zero with the minus one. 115 00:07:29,230 --> 00:07:35,590 And then the respective characters will be added from this my string, and that is we are returning 116 00:07:35,590 --> 00:07:36,880 it using this ILD. 117 00:07:36,880 --> 00:07:42,660 So now we are just accumulating that, returning at the return sets of this particular method. 118 00:07:42,670 --> 00:07:43,790 That is the reverse string. 119 00:07:44,260 --> 00:07:46,600 So now if we go for Fourcade. 120 00:07:46,740 --> 00:07:52,670 In a reverse string world, and if you go for the sprint card, you see the reverse string is actually 121 00:07:52,670 --> 00:07:59,940 returning one iterator and this iterator from this integrated retrieving character by character, using 122 00:07:59,940 --> 00:08:07,680 this for each loop, using this Falu and we are printing this deal are all w there is a reverse of the 123 00:08:08,220 --> 00:08:13,330 that is a character's reverse order of the characters existing in the world in the world text. 124 00:08:13,710 --> 00:08:19,110 So in this way, I've shown you that there is no need to handle arrays, any stop iteration. 125 00:08:19,410 --> 00:08:25,130 There is no need to handle this next method or over this or define this item method. 126 00:08:25,290 --> 00:08:31,980 Everything will be handled by this generator and there is a nice way to handle our in Python. 127 00:08:32,790 --> 00:08:34,050 Thanks for watching this video.