1 00:00:00,090 --> 00:00:04,180 In this tutorial we will talk about generators. 2 00:00:04,530 --> 00:00:08,780 Generators in Python are just another way of creating. 3 00:00:08,790 --> 00:00:19,530 I did a bill objects and they are usually used when we need to create a digital objects quickly without 4 00:00:19,530 --> 00:00:21,790 the need of creating a class. 5 00:00:21,900 --> 00:00:24,620 Implementing the idea data protocol. 6 00:00:24,750 --> 00:00:34,770 Just like we do for I.T. raters and in order to create a generator we just need to define a function 7 00:00:35,070 --> 00:00:43,520 and then use the key word and instead of the return statement 8 00:00:46,250 --> 00:00:53,870 including the yield statement in a function automatically signals to Python that this function will 9 00:00:53,870 --> 00:01:02,810 create a generator in order to create a custom identical object using either it does we define a class 10 00:01:03,140 --> 00:01:15,430 and then implement the special methods I did and next coming to a generator function we define the generator 11 00:01:15,430 --> 00:01:23,720 function using the def key word and the genitive to function accepts parameters 12 00:01:26,910 --> 00:01:39,140 and instead of using a written statement in the generator function we use the yield statement the yield 13 00:01:39,180 --> 00:01:47,160 statement is similar to a return statement in a normal function but whereas a return statement causes 14 00:01:47,160 --> 00:01:54,410 the current environment to be destroyed after the function exits the statement. 15 00:01:54,510 --> 00:02:04,860 Suspend these functions execution and sends a value back to the caller but it retains this date to enable 16 00:02:04,860 --> 00:02:13,010 the function to resume vetted left of and when the function continues execution. 17 00:02:13,010 --> 00:02:17,780 Immediately after the last yield run. 18 00:02:17,810 --> 00:02:25,610 So this allows the its code to produce a series of values or what time rather than computing them at 19 00:02:25,610 --> 00:02:33,360 once and sending them back like a list we should use the yield statement when we want to either it or 20 00:02:33,360 --> 00:02:42,600 word or sequence but don't want to store the entire sequence in the memory so we will discuss two examples. 21 00:02:42,600 --> 00:02:51,530 The first example is a normal function which is going to return the value of the constant squared and 22 00:02:51,530 --> 00:02:54,030 then we also have a generator function. 23 00:02:54,040 --> 00:03:01,290 Here you can see the yield keyword has been used in this function making it a generator function. 24 00:03:01,640 --> 00:03:13,630 So we will first execute did not mean a function and then we will make a function called so when we 25 00:03:13,630 --> 00:03:22,640 define a normal function and call this function the code inside this function gets executed immediately 26 00:03:23,790 --> 00:03:28,900 but coming to a generator function let's see what happens. 27 00:03:28,920 --> 00:03:34,590 So we will first we have defined the generator function. 28 00:03:34,640 --> 00:03:43,410 Now we will make a function call so we have made a function call and assign this function call to a 29 00:03:43,410 --> 00:03:56,520 variable lit display this variable in so when we call a generator function it will not immediately execute 30 00:03:56,550 --> 00:04:04,880 the code in the function but instead it will return a generated object. 31 00:04:04,880 --> 00:04:12,020 Now if you want to execute the code inside the generator function you need to call the next function 32 00:04:12,120 --> 00:04:15,130 or on the generator object. 33 00:04:15,230 --> 00:04:26,780 So let's parse the object just to then execute the new call the next function for the first time on 34 00:04:26,780 --> 00:04:28,730 the generator object. 35 00:04:28,850 --> 00:04:38,800 Execution begins at the start of the function body and continues until the next statement. 36 00:04:39,700 --> 00:04:48,400 And the value to the right of the evil statement is returned and this evil statement inside the function 37 00:04:48,700 --> 00:04:58,950 causes the environment to be saved until the next time the next function is called so that the generator 38 00:04:58,980 --> 00:05:07,290 can keep track of the ideation st so in this example we are not actually generating a sequence. 39 00:05:07,290 --> 00:05:15,020 This is only to show you the difference between a normal function and a generator function so when we 40 00:05:15,020 --> 00:05:26,690 call the next function on the generator object again since there are no widely used to be computed the 41 00:05:26,750 --> 00:05:39,610 stop iteration Edward is raised so this informs us that all the values have been yielded the special 42 00:05:39,620 --> 00:05:49,270 methods ITER and next are implemented automatically so we can either through these items generated using 43 00:05:49,270 --> 00:06:00,080 the generated object using next so the generator function has a lot cleaner code compared to an idea 44 00:06:00,080 --> 00:06:00,560 later. 45 00:06:01,290 --> 00:06:03,590 Let us now consider another example. 46 00:06:03,930 --> 00:06:11,950 And this generator function has multiple yield keyword module statements. 47 00:06:11,980 --> 00:06:20,680 So we need to first create a generator object and then in order to execute the code inside the generator 48 00:06:20,680 --> 00:06:26,320 function out in order to compute the sequence generated by the generated object. 49 00:06:26,470 --> 00:06:35,760 We need to call the next function which gets implemented automatically so let's parse the generated 50 00:06:35,820 --> 00:06:43,800 object to the next function and the first statement and about generator function is an E statement which 51 00:06:43,800 --> 00:06:54,360 is going to return the value of the integer value 1 and we know the E statement inside the function 52 00:06:54,630 --> 00:07:03,170 causes the environment to be saved until the next time the next function is called so the function is 53 00:07:03,170 --> 00:07:15,330 paused after returning the integer value one from the first e statement and then after yielding this 54 00:07:15,330 --> 00:07:20,240 value the function is parsed and the control is transferred to the call. 55 00:07:20,310 --> 00:07:32,050 Now once again when you call the next function on the generator object the execution picks from Betty 56 00:07:32,100 --> 00:07:35,020 Previously executed yield statement. 57 00:07:35,030 --> 00:07:46,840 Left off so the execution begins from the second e-mailed statement and this continues until the next 58 00:07:46,840 --> 00:07:56,850 yield statement is encountered so the second yield statement is going to return the integer value to 59 00:07:57,540 --> 00:08:00,020 and the function is passed. 60 00:08:00,030 --> 00:08:02,750 The control is transferred to the caller. 61 00:08:02,760 --> 00:08:13,580 Now when the call the next function again the execution of the function begins from detoured yield statement 62 00:08:14,060 --> 00:08:20,790 which is going to return the integer while you three and they function is again post. 63 00:08:20,850 --> 00:08:25,240 So this is the generator function that we have discussed about. 64 00:08:25,320 --> 00:08:29,220 So we are going to execute this generator function. 65 00:08:29,220 --> 00:08:38,190 We will first create the generator object so days to underscore or b g is a generated object and we 66 00:08:38,190 --> 00:08:48,180 will pass this object to the next function so when we call the next function for the first time the 67 00:08:48,180 --> 00:08:57,660 first statement in the function body which is the evil statement has returned the value 1 and the function 68 00:08:57,690 --> 00:09:04,670 has been temporarily suspended and the control is given back to the caller. 69 00:09:04,700 --> 00:09:07,860 Now let's call the next function again. 70 00:09:08,400 --> 00:09:15,990 When you call these next function for the second time the function is resumed where it left off. 71 00:09:16,230 --> 00:09:24,700 So the function execution begins from the second statement in the function body which is the yield statement 72 00:09:24,760 --> 00:09:34,590 again and this is returned the integer value to the function gets temporarily suspended and the control 73 00:09:34,620 --> 00:09:36,420 goes back to the caller. 74 00:09:36,420 --> 00:09:44,550 Now let's call the next function for the third time and the last e to statement as eternity the integer 75 00:09:44,670 --> 00:09:45,170 value. 76 00:09:45,190 --> 00:09:55,260 3 No let's call the next function again because there are no values to you from this generated function. 77 00:09:55,350 --> 00:10:04,170 The next function has raised a stop ideation error and here here's another example earlier we have generated 78 00:10:04,200 --> 00:10:08,780 an edit medic sequence using an AI data. 79 00:10:08,880 --> 00:10:14,280 In this example we are generating an added metric sequence using a generator. 80 00:10:14,310 --> 00:10:20,850 We are using a counter in this example in order to keep track of the number of terms to be generated 81 00:10:20,940 --> 00:10:33,390 in the sequence so let us go ahead and create a generated object let's pass the argument the first two 82 00:10:33,490 --> 00:10:34,440 as one. 83 00:10:34,840 --> 00:10:42,100 The number of terms to be generated as some 5 and the difference between the consecutive terms should 84 00:10:42,100 --> 00:10:46,270 be three sigma this. 85 00:10:46,350 --> 00:10:50,880 Now let's call the next function on this object. 86 00:10:50,880 --> 00:10:59,340 So when you call the next function on the generated object the function body gets executed. 87 00:11:00,490 --> 00:11:04,670 Under the first yield statement is encountered. 88 00:11:04,870 --> 00:11:12,640 So we have initialized accounted with devalue one in order to keep track of the number of storms that 89 00:11:12,640 --> 00:11:14,070 are getting generated. 90 00:11:14,230 --> 00:11:23,230 Now since the value of the counter is less than the number of terms to be generated the while loop gets 91 00:11:23,230 --> 00:11:33,950 executed and the value in the arithmetic sequence is generated using this formula and this value is 92 00:11:33,950 --> 00:11:39,050 yielded so that is the value one year. 93 00:11:39,060 --> 00:11:49,960 Now let's make a call to the next function again so subsequent calls to the next function continue from 94 00:11:49,960 --> 00:11:57,850 the Eedle statement to the end of the function and loop around and continue from the start of the function 95 00:11:57,850 --> 00:12:02,290 body until another field statement is called. 96 00:12:02,350 --> 00:12:07,140 When you call the next function for the second time the execution begins. 97 00:12:07,210 --> 00:12:13,180 From this point so the counter is incremented by 1. 98 00:12:13,360 --> 00:12:22,140 We're going to print the value of this counter which is 2 and then since the value of the counted is 99 00:12:22,140 --> 00:12:28,590 less than the number of terms to be generated with just 5 we're going to calculate the next value in 100 00:12:28,590 --> 00:12:29,700 this sequence. 101 00:12:29,700 --> 00:12:39,830 Using this formula and then by yielding this value so subsequent calls to the next function continue 102 00:12:39,830 --> 00:12:48,530 from the yield statement to the end of the function and loops around and continue from the start of 103 00:12:48,530 --> 00:12:57,110 the function body until the Elan statement is encountered let's call the next function again the next 104 00:12:57,110 --> 00:13:05,390 value in the sequence is 7 and the counter has been incremented to 3 calling the next function. 105 00:13:05,390 --> 00:13:16,280 Again the next value generated in the sequence is 10 to 14 because the counter cannot be greater than 106 00:13:16,280 --> 00:13:18,590 the number of terms to be generated. 107 00:13:18,590 --> 00:13:24,500 The total number of terms to be generated is five after generating all the values and if there are no 108 00:13:24,500 --> 00:13:30,290 more values to be generated the stop ideation method is being raised. 109 00:13:30,470 --> 00:13:34,160 And this is about generators in Python.