1 00:00:00,150 --> 00:00:09,930 Hello welcome to this material on I traitors and generators before jumping into traitors and generators. 2 00:00:09,930 --> 00:00:11,920 We will first discuss about. 3 00:00:11,970 --> 00:00:18,230 I did a bit and we have worked on a number of item builds in a previous tutorials. 4 00:00:18,270 --> 00:00:20,480 So what is an item. 5 00:00:20,700 --> 00:00:27,220 And I did a bill is an object that can be looped or word using a for loop. 6 00:00:27,230 --> 00:00:37,740 All I tribals have the magic method double underscored ITER method defined and most of the built in 7 00:00:37,740 --> 00:00:48,220 containers in python like Lists Tuples strings set dictionaries and files and generators. 8 00:00:48,390 --> 00:00:51,600 Add examples of attributes. 9 00:00:51,660 --> 00:00:56,970 Let us now iterate over the elements in a list using a for loop. 10 00:00:56,970 --> 00:01:06,090 So in this example we have defined a list with four elements default loop retrieves each item from the 11 00:01:06,090 --> 00:01:14,430 list perform some operations and displays the output and when they for loop reaches the end of the list 12 00:01:14,940 --> 00:01:17,600 that is if there are no more items in the list. 13 00:01:17,730 --> 00:01:20,930 The for loop gets dominated. 14 00:01:20,940 --> 00:01:24,770 So let us execute this code in the cell. 15 00:01:24,780 --> 00:01:27,420 And here is the output. 16 00:01:27,420 --> 00:01:29,220 Now let's see another example. 17 00:01:29,280 --> 00:01:37,200 So we have defined a dictionary dict 1 and using a for loop we want to loop over the keys that are defined 18 00:01:37,320 --> 00:01:39,090 in the dictionary. 19 00:01:39,090 --> 00:01:47,520 So using the for loop here we will traverse over the keys in the dictionary. 20 00:01:47,520 --> 00:01:50,800 So let's execute the code in this cell. 21 00:01:51,360 --> 00:02:00,290 So the for loop has retrieved each of the keys in the dictionary and then displayed them in the output. 22 00:02:00,420 --> 00:02:08,660 Next we can also I did it over the content in a file object using a for loop. 23 00:02:08,760 --> 00:02:20,570 So here in this example we have first opened the file and then using the for loop we are going to read 24 00:02:20,570 --> 00:02:27,260 the contents of the file line by line and then return the same. 25 00:02:27,260 --> 00:02:41,610 Using the for loop so let's execute the code in this cell and see the output so the for loop read the 26 00:02:41,610 --> 00:02:45,590 file line by line and then returns the same. 27 00:02:45,660 --> 00:02:53,010 We have also seen a number of examples maybe used a for loop to iterate or what the sequence of numbers 28 00:02:53,460 --> 00:02:56,880 generated using the range function. 29 00:02:57,860 --> 00:03:04,280 The range function returns a Range object which again is an item. 30 00:03:04,910 --> 00:03:09,640 So we can use a for loop to iterate over the sequence of numbers. 31 00:03:09,680 --> 00:03:18,720 So let's execute the code in this is so the for loop has either dated or what the sequence of numbers 32 00:03:18,750 --> 00:03:21,490 generated by the range function. 33 00:03:21,720 --> 00:03:31,650 A while loop can also be used to loop or word and I durable and this action the ideation continues until 34 00:03:31,650 --> 00:03:34,160 the test expression in our example. 35 00:03:34,170 --> 00:03:44,270 This is the test expression so until this takes expression becomes false at this point the program execution 36 00:03:44,280 --> 00:03:47,550 proceed to the first statement after the loop body. 37 00:03:48,140 --> 00:03:57,080 So we have defined a list here with three elements and we want to iterate over a loop or what the elements 38 00:03:57,620 --> 00:03:58,820 in the list. 39 00:03:58,910 --> 00:04:06,470 Using a while loop then the while loop reaches the end of the list the program execution should be terminated. 40 00:04:06,680 --> 00:04:16,700 So for that we have this condition here and in denote the index number of the list that has X denote 41 00:04:16,730 --> 00:04:18,500 the length of the list. 42 00:04:18,530 --> 00:04:20,740 So let's execute this piece of code. 43 00:04:23,080 --> 00:04:31,930 So the while loop as I iterated all the elements in the list and then display the items next. 44 00:04:32,140 --> 00:04:42,120 The membership operator in can also be used to loop or would I dribbled say for example you want to 45 00:04:42,120 --> 00:04:54,720 check if the integer 1 exists in the list containing the elements C a string a and then floating point 46 00:04:54,720 --> 00:05:07,660 number three 2 at Ford and then string again and then the numbered or the integer 1 so let's execute 47 00:05:07,660 --> 00:05:09,190 this piece of code. 48 00:05:09,340 --> 00:05:18,310 In this example the in operator has either rated or what the elements in this list not to check if the 49 00:05:18,310 --> 00:05:22,270 integer 1 exists in the list here and it has returned true. 50 00:05:22,570 --> 00:05:31,330 So we have seen how we can use a for loop to either it or word a number of containers built in containers 51 00:05:31,330 --> 00:05:34,070 like lists dictionaries etc.. 52 00:05:34,150 --> 00:05:35,740 Now let us try to. 53 00:05:35,740 --> 00:05:40,110 I did it or were an integer object. 54 00:05:41,400 --> 00:05:47,180 C for x in 0 5 int x. 55 00:05:47,250 --> 00:05:52,980 So here Python has stored a type error seeing integers. 56 00:05:53,010 --> 00:05:55,830 Object is not ideal. 57 00:05:56,070 --> 00:05:57,810 So the for loop. 58 00:05:57,810 --> 00:06:03,450 Can I did it all would I durables only and not any other object. 59 00:06:03,750 --> 00:06:14,760 So what makes an object and I travel all like the rebels shared a common thread which is they implement 60 00:06:14,850 --> 00:06:25,320 the double underscored ITER method the Dunder method or the magic method ITER is implemented by all 61 00:06:25,390 --> 00:06:26,790 AI durable objects. 62 00:06:27,330 --> 00:06:35,970 So basically a neat object within a tenable method is called an item in this particular object can be 63 00:06:35,970 --> 00:06:45,960 used in a for loop and like with most magic method we cannot call the double underscored I did method 64 00:06:46,140 --> 00:06:57,000 directly if you want to call a method defined in a class on an instance you can use the dot operator 65 00:06:58,260 --> 00:07:04,210 but most magic method should not be called directly. 66 00:07:04,290 --> 00:07:08,610 Instead we can use the function ie the e 67 00:07:11,370 --> 00:07:21,090 the idea function in done called the magic method w underscored ITC. 68 00:07:21,540 --> 00:07:29,910 Now if you call the I do the function and pass and I did a bill like a list. 69 00:07:29,910 --> 00:07:33,230 This is it don't and I iterated. 70 00:07:33,480 --> 00:07:36,090 So what is and I did don't. 71 00:07:36,150 --> 00:07:47,050 So what does an I did it a night to day did is an object which represents a stream of data and a night 72 00:07:47,070 --> 00:07:49,000 the data allows us to. 73 00:07:49,020 --> 00:07:57,450 I did it or what the contents of a container a nitrate that as an object in Python that behaves something 74 00:07:57,450 --> 00:08:03,850 like a list so it is a collection of values one after the other. 75 00:08:04,300 --> 00:08:15,130 And the item data object can be invoked or were to produce a series of values and someone using the 76 00:08:15,670 --> 00:08:25,500 double underscore next method but then I did it that object differs from a list in that the values are 77 00:08:25,500 --> 00:08:28,120 not stored in your computer memory. 78 00:08:28,120 --> 00:08:33,820 Instead they're calculated on the fly which saves us a lot of memory. 79 00:08:33,820 --> 00:08:44,080 Say for example when you define a list or a set dictionary to pull out a string in Python this action 80 00:08:44,270 --> 00:08:55,450 allocate a specific block of memory so all these data structures live in the memory and also hold all 81 00:08:55,450 --> 00:08:57,770 their values in the memory. 82 00:08:58,000 --> 00:09:07,480 But coming to AI traitors they do not compute the value of each item when instantiated the line used 83 00:09:07,630 --> 00:09:14,320 in a CDO or the sequence are computed only when you ask for it. 84 00:09:14,320 --> 00:09:18,250 And this is known as lazy evaluation. 85 00:09:19,590 --> 00:09:26,930 And lazy evaluation is useful when you have a very large dataset to compute. 86 00:09:27,150 --> 00:09:34,860 It allows us to start using the data immediately while the whole dataset is being computed so it allows 87 00:09:34,860 --> 00:09:41,160 us to manipulate large amounts of data while keeping a very little bit of actually in the memory. 88 00:09:41,550 --> 00:09:51,360 So I did does act like this but they do not take up space so a Knight-Ridder looks like a sequence when 89 00:09:51,360 --> 00:09:55,990 we use or generate items or elements from it. 90 00:09:56,100 --> 00:10:02,610 But there are some major differences so if you want to construct a sequence like object. 91 00:10:02,730 --> 00:10:08,250 So in order to construct a sequence like object you need to write a function. 92 00:10:08,250 --> 00:10:17,210 Every element of the custom either data object is actually produced only when we ask for it. 93 00:10:17,490 --> 00:10:27,200 And then unlike the sequences or I troubles and I iterator sequence can be infinitely long. 94 00:10:27,450 --> 00:10:33,840 So they can generate while use which infinitely long 95 00:10:37,510 --> 00:10:45,850 in python I did it those that implemented as classes say for example you want to build a custom. 96 00:10:45,850 --> 00:10:56,640 I did it object that looks like a sequence and this object generate infinite numbered all values. 97 00:10:56,890 --> 00:11:08,270 And for that you need to construct a class let's see the class name is best and then you need to define 98 00:11:08,570 --> 00:11:10,920 do method in this class. 99 00:11:10,970 --> 00:11:18,360 One is the double underscore either method and the other is double underscored. 100 00:11:18,380 --> 00:11:20,330 Next method. 101 00:11:20,330 --> 00:11:31,380 So in order to define a class which is an idea to build the class must implement these two methods the 102 00:11:31,500 --> 00:11:43,070 ITER method and then next met the SO OFTEN defining the different methods in the class we create an 103 00:11:43,130 --> 00:11:46,220 instance of this class test. 104 00:11:46,370 --> 00:11:55,310 Say for example our instance is object or B J one which is an instance of this class test. 105 00:11:55,400 --> 00:12:00,770 Now next you want to generate a series of values. 106 00:12:00,770 --> 00:12:09,610 Now if you want to generate a value from and then find entities like this we should call the magic method. 107 00:12:09,620 --> 00:12:18,920 Next on the instantiated object in order to generate a value so we need to use the next function instead 108 00:12:18,920 --> 00:12:21,520 of directly calling the magic method. 109 00:12:21,560 --> 00:12:22,870 Double underscore next. 110 00:12:22,910 --> 00:12:31,750 So we will call the next function and parse the instance of the class or b 1. 111 00:12:31,850 --> 00:12:38,660 The next function or the next method that you have to find in the class has logic. 112 00:12:38,660 --> 00:12:47,840 In order to compute the first while you in the infinite sequence and this will return the first element 113 00:12:47,900 --> 00:12:50,850 in the infinite sequence which is 1. 114 00:12:50,870 --> 00:12:57,500 Now if you want to generate the next value in the sequence we need to call the next method. 115 00:12:57,500 --> 00:13:06,800 Again using the next function and pass the instance to this function the next method that you have defined 116 00:13:06,890 --> 00:13:16,820 in the class compute the next value in the sequence and returns forward so we can loop all word custom 117 00:13:17,040 --> 00:13:25,200 iterator objects using the double underscore ITER and then double underscore next methods.