1 00:00:00,180 --> 00:00:10,500 In this tutorial we will talk about Paul a use for iterating over a set of statements repeatedly in 2 00:00:10,500 --> 00:00:11,400 Python. 3 00:00:11,400 --> 00:00:22,130 There are three types of Lew The for loop while and do y Lew a for loop is used for either dating or 4 00:00:22,130 --> 00:00:28,680 what a sequence such as this a string a tuple or addiction. 5 00:00:30,230 --> 00:00:40,040 And this is the syntax for the for loop for variable in sequence and we have a set of statements after 6 00:00:40,140 --> 00:00:50,050 the colon so if you want to either iterate or what each of the characters in a string you can use the 7 00:00:50,050 --> 00:00:50,750 for loop. 8 00:00:51,510 --> 00:01:00,210 So here I have used a far loop to either did or what a string for i in Python. 9 00:01:00,870 --> 00:01:10,530 And then like we do for it and if statement or the if clause use goal in here and then I have a print 10 00:01:10,740 --> 00:01:22,780 statement indeed for loop as you can see we have indented the statement in default loop like we do for 11 00:01:22,780 --> 00:01:23,030 it. 12 00:01:23,130 --> 00:01:31,580 And if clause with the for loop we can execute a set of statements for each item in a list. 13 00:01:32,680 --> 00:01:36,200 Tuple set out a string. 14 00:01:36,210 --> 00:01:43,610 Now let's consider the second example here in this example I want to I did it or would a list. 15 00:01:43,610 --> 00:01:50,370 So this is the statement that I'm going to use for x in this particular list containing the elements 16 00:01:50,370 --> 00:02:00,930 1 2 3 4 5 so for all elements in the list I want to squared each of the items and then print the result 17 00:02:00,930 --> 00:02:01,240 here. 18 00:02:02,820 --> 00:02:08,880 So for each item in the list this action is performed. 19 00:02:08,880 --> 00:02:19,450 And then finally it this printed the items of the sequence object are assigned one after the other to 20 00:02:19,450 --> 00:02:28,730 the loop variable that is in case of our example the item in the string that is B and Y. 21 00:02:28,740 --> 00:02:40,030 D hedge or n are assigned to the loop variable ie in a sequence first it points to the element B and 22 00:02:40,030 --> 00:02:53,580 then it goes to Y and then t h and so on so to be precise the variable points to the items and for each 23 00:02:53,700 --> 00:02:59,050 item the code in the for loop that executed. 24 00:02:59,130 --> 00:03:07,680 So in the second example the loop variable X points to 1 first and then it moves to the second item 25 00:03:07,710 --> 00:03:17,130 2 and then 3 and so on but let's look at two examples in the Jupiter note so in order to iterate over 26 00:03:17,130 --> 00:03:21,690 the elements in the string I'm going to use the for loop. 27 00:03:21,900 --> 00:03:28,680 And finally I'm going to print all of the elements in the string so let's execute this cell. 28 00:03:29,610 --> 00:03:39,300 So the far loop has used do loop bearable to pick each of the elements in the string and then in the 29 00:03:39,300 --> 00:03:44,770 body or the for loop we wanted to print each of these elements. 30 00:03:44,820 --> 00:03:46,770 Now let's look at another example. 31 00:03:50,690 --> 00:04:01,370 The for loop is going to pick each item in the list and apply the same set of statements for each item 32 00:04:01,460 --> 00:04:02,390 in the list. 33 00:04:02,390 --> 00:04:10,190 So another example it's going to pick the item one graded and then print the result it's going to Big 34 00:04:10,190 --> 00:04:15,460 D second item two squared in number two and then print it as well. 35 00:04:15,530 --> 00:04:16,460 And so on. 36 00:04:16,490 --> 00:04:26,530 Not let execute the cell so as you can see the same set of statements have been applied on each element 37 00:04:26,620 --> 00:04:27,180 of the list. 38 00:04:30,840 --> 00:04:42,020 In the second example I have passed the Range object to the list constructor and using the for loop 39 00:04:42,110 --> 00:04:52,610 I am going to iterate over the D sequence of items in the list fort I in list and I have passed the 40 00:04:52,610 --> 00:04:59,690 argument range of 10 and in the for loop body I'm going to put in the items in the list. 41 00:04:59,960 --> 00:05:11,260 So after I execute this said for loop has printed the items in the list beginning from 0 to 9. 42 00:05:13,160 --> 00:05:23,060 Coming back to the for loop for loop can have an optional X block both related with it and the 8 block 43 00:05:23,150 --> 00:05:28,990 executes only when the for loop has executed all the iterations. 44 00:05:29,000 --> 00:05:38,320 So in this example I have used an alias block along with the for loop and the L statement is executed. 45 00:05:38,320 --> 00:05:47,390 Then the for loop has exhausted either dating or were the items in the or the element in the string. 46 00:05:47,500 --> 00:05:53,460 The IDS blog it executed with de loop I iteration complete normally. 47 00:05:54,250 --> 00:05:59,490 So let's execute this piece of code in the Jupiter notebook. 48 00:05:59,560 --> 00:06:08,600 Here in this example we are using an edited block with a for loop using the loop where it will go into 49 00:06:08,620 --> 00:06:16,570 irate all the items in the trim print the items and after I iterating all over all the items in the 50 00:06:16,570 --> 00:06:22,700 string the control goes to the else blog and the statement in the else block executed. 51 00:06:23,230 --> 00:06:30,430 So when you execute this piece of code you have the items in the string printed first all of the items 52 00:06:30,640 --> 00:06:37,400 and finally the print statement in the edit block is printed. 53 00:06:37,430 --> 00:06:45,850 Next we're going to discuss about nested loops for loops present inside and not good for loop then it 54 00:06:45,850 --> 00:06:55,390 does call a nested for loop when units do lose the outer loop takes control of the number of complete 55 00:06:55,630 --> 00:06:57,850 iterations of the inner loop. 56 00:06:58,000 --> 00:06:59,500 So let's understand this 57 00:07:03,410 --> 00:07:07,570 so here is an example demonstrating nested loops. 58 00:07:08,000 --> 00:07:16,310 So this is loop and Mike loop I've used a range function to generate the numbers and this range function 59 00:07:16,310 --> 00:07:24,620 is going to generate the numbers 0 1 and 2 next. 60 00:07:24,640 --> 00:07:35,290 This is the inner loop the inner loop I'd read or would a string containing the characters a B and C 61 00:07:36,590 --> 00:07:43,790 so the inner loop has a print function that brings each of the elements. 62 00:07:44,000 --> 00:07:54,990 And finally after we come out of the inner Lew I have given a print function for every single ideation 63 00:07:55,010 --> 00:08:04,900 of the outer loop several ideations of the inner loop performed for one ideation of the outer loop. 64 00:08:05,120 --> 00:08:11,010 The dire cycle of ideations is completed in the inner cycle. 65 00:08:11,030 --> 00:08:20,900 This piece of code meaning the inner loop print the elements a b and c. 66 00:08:21,080 --> 00:08:25,180 So this completes one cycle of the inner loop. 67 00:08:25,460 --> 00:08:35,870 And here I have given a print function to show you that the outer loop has completed only the first 68 00:08:35,890 --> 00:08:36,970 ideation. 69 00:08:37,120 --> 00:08:46,960 So this brain function is going to print this text like this zero times and then the control goes back 70 00:08:46,960 --> 00:08:49,660 to the inner loop again. 71 00:08:49,820 --> 00:09:00,390 The second cycle again print a B and C and now control comes to the outer loop one time 72 00:09:03,020 --> 00:09:04,160 and then so on. 73 00:09:04,160 --> 00:09:06,630 So this is the first cycle. 74 00:09:06,740 --> 00:09:15,350 Second cycle and we have three such cycles like that so let's execute this piece of code in the Jupiter 75 00:09:15,350 --> 00:09:16,000 notebook. 76 00:09:19,020 --> 00:09:28,420 So as this goes to earlier I have used two loops to demonstrate nested loops for one cycle. 77 00:09:28,420 --> 00:09:33,040 That is for one complete iteration of the inner loop. 78 00:09:33,100 --> 00:09:41,400 One ideation of the outer loop is performed let's execute this piece of code the inner loop has rendered 79 00:09:41,480 --> 00:09:46,590 the each of the characters in the string ABC. 80 00:09:46,870 --> 00:09:52,240 The first itemization of the outer loop print this text next. 81 00:09:52,450 --> 00:09:54,050 The second cycle. 82 00:09:54,840 --> 00:10:04,530 Comprising of three ideations of the inner loop is executed and then we have the second by iteration 83 00:10:04,530 --> 00:10:05,840 of the outer loop. 84 00:10:05,940 --> 00:10:12,810 Printing this text next again the third cycle of the inner blueprint. 85 00:10:12,810 --> 00:10:15,090 This text A B and C. 86 00:10:15,090 --> 00:10:23,570 And finally we have the outer loop printing text here which is the final iteration.