1 00:00:13,560 --> 00:00:19,080 Hello to all of my students, and today we will kick off with a new topic, which is Loop's, we will 2 00:00:19,080 --> 00:00:25,860 see what our loop's see, some definitions and theory, and also we will see how can we apply Loop's 3 00:00:25,860 --> 00:00:27,660 in Python programming language. 4 00:00:28,500 --> 00:00:32,970 So, first of all, let's discuss what our four lupe's actually. 5 00:00:33,000 --> 00:00:33,760 What do they mean? 6 00:00:34,140 --> 00:00:34,610 All right. 7 00:00:34,860 --> 00:00:42,930 So in computer science, a loop is a control flow statement for specified specifying iteration. 8 00:00:43,500 --> 00:00:43,740 Right. 9 00:00:43,830 --> 00:00:50,400 What iteration means is basically that we want to apply the same action again and again and again. 10 00:00:50,580 --> 00:00:53,130 And we don't want to print it. 11 00:00:53,130 --> 00:00:55,710 We don't want to write it separately. 12 00:00:56,010 --> 00:01:03,300 If we have the same action to be applied, we can do it in one or two lines instead of doing it in hundreds 13 00:01:03,300 --> 00:01:05,460 of lines, for example, if it's the same action. 14 00:01:06,120 --> 00:01:14,050 So a loop is used for executing a block of statements repeatedly until a particular condition is satisfied. 15 00:01:14,580 --> 00:01:23,370 So you probably recall this sentence for from when we studied if statements, we talked about condition 16 00:01:23,370 --> 00:01:24,630 satisfaction there. 17 00:01:24,990 --> 00:01:30,030 If something, then do something and else do something else. 18 00:01:30,240 --> 00:01:36,720 So airconditioned satisfaction is actually belongs to if statements and exactly that. 19 00:01:36,720 --> 00:01:38,790 I said what I said in previous lectures. 20 00:01:38,910 --> 00:01:46,560 You will see as long as we go further with the course that all topics come together and they are used 21 00:01:46,560 --> 00:01:54,600 together as a machine and especially in for loops you will see that we can use for loops and other loops 22 00:01:54,600 --> 00:01:55,110 as well. 23 00:01:55,440 --> 00:02:01,920 You will see how can we use a loop combined with an IF statement and combined with collections, for 24 00:02:01,920 --> 00:02:04,950 example, they work perfectly together. 25 00:02:05,100 --> 00:02:06,570 They were made for each other. 26 00:02:07,140 --> 00:02:15,000 This way we can make our code efficient and, you know, just not using spaghetti code. 27 00:02:15,000 --> 00:02:16,770 Spaghetti code is a term in programming. 28 00:02:17,010 --> 00:02:23,380 Once you write a lot of code, which you can write in a lot smaller amount of code. 29 00:02:23,400 --> 00:02:23,780 All right. 30 00:02:23,970 --> 00:02:28,140 So this is spaghetti code and we don't want to write spaghetti code. 31 00:02:28,140 --> 00:02:34,650 We want to write efficient short code as possible, because later on, as you build your projects in 32 00:02:34,650 --> 00:02:43,020 your career, as your code is shorter, it makes little it makes less mess to understand it for the 33 00:02:43,020 --> 00:02:43,350 wrong. 34 00:02:44,250 --> 00:02:44,670 All right. 35 00:02:44,680 --> 00:02:51,060 So, for example, let's assume we have a list collection that contains numbers and we want to multiply 36 00:02:51,060 --> 00:02:58,500 each cell value by five and add up until now, we have the tools to do it, but we had the tools to 37 00:02:58,500 --> 00:02:59,820 do it in a primitive way. 38 00:03:00,000 --> 00:03:06,030 And actually it's the wrong way, because once you for example, once you have a collection of lists 39 00:03:06,430 --> 00:03:13,070 list collection and you have three items inside one, two and three integer, very values, very simple. 40 00:03:13,440 --> 00:03:16,470 And I want to multiply each of these values by five. 41 00:03:16,740 --> 00:03:25,230 I basically mention the collection and the list and I want to do a certain action upon cell index zero 42 00:03:25,410 --> 00:03:26,780 and then I have the equals sign. 43 00:03:26,940 --> 00:03:35,340 So whatever comes after this to the right of what I just marked will insert will be inserted to sell 44 00:03:35,340 --> 00:03:36,100 index zero. 45 00:03:36,330 --> 00:03:43,500 So for example, if I write Houtan afterwards, if I print Selinda Zero, I will have the value 10 inside 46 00:03:43,500 --> 00:03:44,390 instead of one. 47 00:03:44,970 --> 00:03:50,220 But in our case I'm just thinking the cell and multiplying what's inside by five. 48 00:03:51,180 --> 00:03:57,510 So after this action will be executed, if I print sell index zero, I will have the value five in here. 49 00:03:58,230 --> 00:04:00,210 Same thing goes for here. 50 00:04:00,420 --> 00:04:05,310 Selling the X1 multiplied by five and sound index two multiplied by five. 51 00:04:06,120 --> 00:04:11,820 So basically we have three lines here for three separate actions. 52 00:04:11,820 --> 00:04:16,390 Once you can you can see here that all of them are just multiplied by five. 53 00:04:16,410 --> 00:04:17,220 Again and again. 54 00:04:18,300 --> 00:04:20,550 Now let's see the efficient way to do it. 55 00:04:20,820 --> 00:04:21,900 Don't get scared. 56 00:04:22,500 --> 00:04:26,610 If you see now, you will see no for loops, probably for the first time. 57 00:04:26,850 --> 00:04:32,520 Or maybe you have encountered full in other programming languages or in some articles. 58 00:04:32,880 --> 00:04:40,110 But right after we see the quick example here, we will discuss thoroughly Waterfall Loops and how to 59 00:04:40,110 --> 00:04:44,140 use them and what what each what each word here means. 60 00:04:44,160 --> 00:04:44,480 All right. 61 00:04:45,330 --> 00:04:47,550 So Option B, which is the right way. 62 00:04:48,600 --> 00:04:50,550 You can see here a list. 63 00:04:50,670 --> 00:04:51,090 All right. 64 00:04:51,390 --> 00:04:52,800 For now is the same list. 65 00:04:53,310 --> 00:05:00,390 And here we will see the keyword for which indicates that we are using a for loop. 66 00:05:00,610 --> 00:05:01,040 All right. 67 00:05:01,350 --> 00:05:07,320 And whatever comes right after is the items which will be iterated inside the for loop. 68 00:05:08,080 --> 00:05:12,540 So before talking about cell and in will just. 69 00:05:12,750 --> 00:05:19,770 Look at this thing you see here, numbers, and you see that this is the name of the list, so remember 70 00:05:19,770 --> 00:05:27,480 that whatever comes after the in, whatever comes after the in is the whole package, the whole package 71 00:05:27,480 --> 00:05:30,400 that we will be focused on inside the four. 72 00:05:30,420 --> 00:05:31,950 Look, this is the whole package. 73 00:05:32,190 --> 00:05:40,200 And whatever comes after the four is the single item inside the whole package. 74 00:05:40,590 --> 00:05:40,980 All right. 75 00:05:40,990 --> 00:05:45,530 So in our case, we have here the list, which is the whole package. 76 00:05:45,540 --> 00:05:46,830 We have three items here. 77 00:05:47,010 --> 00:05:47,370 All right. 78 00:05:47,400 --> 00:05:48,480 This is the whole package. 79 00:05:49,380 --> 00:05:50,760 It comes after the Æon. 80 00:05:51,300 --> 00:05:55,550 And what comes after the four is the single item. 81 00:05:55,560 --> 00:06:00,960 So in our case, cell indicates the cell value. 82 00:06:01,860 --> 00:06:02,250 All right. 83 00:06:02,250 --> 00:06:04,850 Cell indicates the cell value. 84 00:06:05,310 --> 00:06:12,870 So you can see here that I'm taking the cell value and I'm taking its existing value and multiplying 85 00:06:12,870 --> 00:06:13,650 it by five. 86 00:06:14,040 --> 00:06:20,430 And it happens again and again and again until the whole items here are iterated. 87 00:06:21,180 --> 00:06:21,650 All right. 88 00:06:21,960 --> 00:06:29,820 So basically, we're taking the same action that we did right here, but we're doing it in two lines 89 00:06:29,940 --> 00:06:36,330 instead of three, or even though if you even if you have here, for example, four and five and six. 90 00:06:36,600 --> 00:06:43,600 So you probably can guess that these lines would grow exponentially. 91 00:06:43,620 --> 00:06:43,940 All right. 92 00:06:44,190 --> 00:06:50,850 Because as we go here, we have zero cell cells, Europe cell one cell to sell three or four and zero 93 00:06:50,850 --> 00:06:51,240 five. 94 00:06:51,570 --> 00:06:53,310 So this thing is going to grow. 95 00:06:53,560 --> 00:06:59,970 And as as we have much more items inside the list, this thing is going to grow. 96 00:06:59,970 --> 00:07:03,270 And it's not good for us because we're going to have spaghetti code here. 97 00:07:03,450 --> 00:07:08,520 And for example, if we have one hundred items here, we're going to have one hundred one hundred lines 98 00:07:08,520 --> 00:07:08,790 here. 99 00:07:08,910 --> 00:07:10,050 And it's not good for us. 100 00:07:10,590 --> 00:07:16,520 But in the follow up case, if we're having here a lot of items, it doesn't matter. 101 00:07:16,710 --> 00:07:23,790 This code stays the same each time that we take the cell and we multiply the cell value on five. 102 00:07:24,010 --> 00:07:24,360 All right. 103 00:07:24,370 --> 00:07:26,100 So it stays two lines. 104 00:07:26,100 --> 00:07:27,900 You see how efficient this is. 105 00:07:28,140 --> 00:07:28,520 All right. 106 00:07:28,770 --> 00:07:30,510 This is the way to use for loops. 107 00:07:30,900 --> 00:07:38,010 And from here, once we've seen the quick example, let's go over and talk about more specifically regarding 108 00:07:38,010 --> 00:07:40,890 for loops, because there are several types of loops. 109 00:07:41,130 --> 00:07:44,100 Let's start off with a for loop, which is the most popular one. 110 00:07:45,180 --> 00:07:53,250 So what these are for loop in programming A for loop is used to iterate several times upon certain items. 111 00:07:53,640 --> 00:08:02,090 It doesn't matter if it's variables, list cells and so on and applying actions in each iteration. 112 00:08:02,220 --> 00:08:08,820 So each time the loop will run in each iteration, we will have a certain action taken. 113 00:08:09,310 --> 00:08:09,740 All right. 114 00:08:09,960 --> 00:08:16,470 At this point we will not discuss if statements inside for loops because it's a bit more advanced. 115 00:08:16,470 --> 00:08:21,200 I think that we'll discuss it in the next lecture on maybe the lecture after. 116 00:08:22,230 --> 00:08:22,730 All right. 117 00:08:22,740 --> 00:08:29,850 So as we saw in the previous example, we can take a collection and apply actions on its items. 118 00:08:30,270 --> 00:08:33,300 So full structure looks like this, right? 119 00:08:34,080 --> 00:08:37,320 You can see here the example from above. 120 00:08:37,380 --> 00:08:41,660 So first of all, you can see the list here, numbers B, one, two, three. 121 00:08:42,000 --> 00:08:49,540 So and here the explanation for loop will be A for loop will extract one cell on each iteration. 122 00:08:49,570 --> 00:08:49,910 All right. 123 00:08:50,430 --> 00:08:58,460 So if the goal here down, you can see the for loop and you can see the word four and then cell. 124 00:08:58,830 --> 00:09:04,420 So the word that comes after four is one cell. 125 00:09:05,040 --> 00:09:05,480 All right. 126 00:09:05,670 --> 00:09:07,140 Choose any words you want. 127 00:09:07,140 --> 00:09:11,910 You can switch this word with box, with item, whatever you want. 128 00:09:12,180 --> 00:09:19,860 It doesn't matter because the cell, the word here, and you can replace it with any word and it indicates 129 00:09:19,860 --> 00:09:25,800 of a single item out of what comes after in. 130 00:09:26,320 --> 00:09:26,700 All right. 131 00:09:26,700 --> 00:09:29,550 So whatever comes after in is the whole package. 132 00:09:30,210 --> 00:09:35,980 And whatever comes after four is a single item out of the whole package. 133 00:09:36,030 --> 00:09:40,650 This is really simple explanation and I think it's really easy to understand as well. 134 00:09:41,310 --> 00:09:49,410 So from here, let's see a live example of the theory that we have just discussed and let's see what's 135 00:09:49,410 --> 00:09:50,390 going on up here. 136 00:09:50,400 --> 00:09:50,750 All right. 137 00:09:51,060 --> 00:09:56,540 So I have the example that we just discussed, option A and option B, the wrong way and the right way. 138 00:09:56,880 --> 00:10:02,970 So this will be the wrong way, this bulk of code, and this will be the right way, this block of code. 139 00:10:03,720 --> 00:10:04,070 All right. 140 00:10:04,080 --> 00:10:09,800 By the way, just just for you to know that if you put quotation marks and inside it, you will put 141 00:10:10,110 --> 00:10:12,360 it and you see it's marked in. 142 00:10:13,000 --> 00:10:22,090 Orange same is four and the same as in it is a saved key, and it means like go one line below an empty 143 00:10:22,090 --> 00:10:24,520 line, go one empty line below. 144 00:10:24,880 --> 00:10:25,180 All right. 145 00:10:25,180 --> 00:10:28,500 Because I want to separate this gold and this gold once they print it out. 146 00:10:30,070 --> 00:10:33,040 So at the top here, you can see the wrong example. 147 00:10:33,340 --> 00:10:37,900 I just declared here a list with three items, same as we discussed. 148 00:10:37,900 --> 00:10:44,550 And I'm just going to take cell zero, sell one and sell two and multiply all of them by five. 149 00:10:44,830 --> 00:10:48,610 And afterwards, I would like to print the output. 150 00:10:48,640 --> 00:10:48,910 Right. 151 00:10:48,940 --> 00:10:53,110 So just going to comment this out, for example, for a second like that. 152 00:10:53,440 --> 00:10:54,580 And let's just run it. 153 00:10:56,220 --> 00:11:02,400 All right, so as you can see, the initial values here are one, two, three, and once I multiply 154 00:11:02,460 --> 00:11:10,830 it by five and the expected values of print right here, print one first print second and third print 155 00:11:11,010 --> 00:11:13,090 are five, 10 and 15. 156 00:11:13,410 --> 00:11:15,370 Basically, these values right here. 157 00:11:15,390 --> 00:11:17,460 Well, multiply by five. 158 00:11:17,940 --> 00:11:18,320 All right. 159 00:11:19,230 --> 00:11:22,170 So this is the way that we've done it until this far. 160 00:11:22,980 --> 00:11:27,900 But right here below declared I you can see the right example. 161 00:11:27,900 --> 00:11:31,740 First of all, I declared a second list, which is called numbers B. 162 00:11:31,920 --> 00:11:35,280 Also, I've set here one to three items inside each. 163 00:11:35,280 --> 00:11:37,050 So you can see here, one, two, three. 164 00:11:37,350 --> 00:11:42,960 And right after I'm going to use the for loop to iterate the same action I did here. 165 00:11:43,110 --> 00:11:48,780 Multiply more motivation by five and I'll just take the four. 166 00:11:49,030 --> 00:11:53,200 You can see it marked in orange, which is a saved keyword. 167 00:11:53,340 --> 00:11:54,840 Same as the comma here. 168 00:11:55,110 --> 00:11:55,530 All right. 169 00:11:55,830 --> 00:11:59,640 So four and then I have after the in. 170 00:11:59,940 --> 00:12:00,380 All right. 171 00:12:00,390 --> 00:12:05,790 As we discussed after the end, I had the whole package and you can see one side double tappet. 172 00:12:06,120 --> 00:12:12,180 The numbers B list is colored as well with a brownish background. 173 00:12:12,630 --> 00:12:19,090 So it indicates the in here what comes after the N gets the value of this list. 174 00:12:20,070 --> 00:12:21,450 So this is the whole package. 175 00:12:21,570 --> 00:12:25,740 And what comes after the four is the single item. 176 00:12:25,740 --> 00:12:28,830 In our case, it's the sale value. 177 00:12:29,010 --> 00:12:29,580 All right. 178 00:12:30,270 --> 00:12:37,080 Right after inside the for loop, once we're doing an indentation, you can see here, I'm thinking 179 00:12:37,180 --> 00:12:44,430 taking the cell value and multiplying it by five and right after, I'm just going to print the value 180 00:12:44,430 --> 00:12:46,340 after it's been multiplied by five. 181 00:12:47,370 --> 00:12:47,820 All right. 182 00:12:48,210 --> 00:12:52,740 So once I run it, all right, you can see here rightway print out. 183 00:12:52,840 --> 00:12:53,910 This is this thing. 184 00:12:54,420 --> 00:12:57,590 And right after I also multiply each sale by five. 185 00:12:57,810 --> 00:12:59,160 So what's happened here? 186 00:12:59,160 --> 00:13:06,260 The for loop, the code, the code, it goes like this we printed right now and then we go to the for 187 00:13:06,270 --> 00:13:06,560 loop. 188 00:13:07,490 --> 00:13:07,940 All right. 189 00:13:08,220 --> 00:13:12,180 So then the for loop took the first item out of this list right here. 190 00:13:13,020 --> 00:13:21,210 It took the first item and multiply it by five and printed the output and then it went again. 191 00:13:21,660 --> 00:13:22,140 All right. 192 00:13:22,140 --> 00:13:27,930 From here, took the second cell out of this whole package, as we mentioned. 193 00:13:28,830 --> 00:13:31,020 And he took this item right here. 194 00:13:31,020 --> 00:13:34,680 The second cell also multiplied by five print out the output. 195 00:13:34,890 --> 00:13:42,280 And then he got to the last cell, which is value three, and also multiply it by five. 196 00:13:42,570 --> 00:13:50,190 So basically what's going on here is the full loop will take all the items one by one in a loop. 197 00:13:50,310 --> 00:13:50,720 All right. 198 00:13:50,730 --> 00:13:57,090 This is why it's called the loop is going to go around and around again and again until each of these 199 00:13:57,090 --> 00:13:59,940 cells separately will be executed. 200 00:14:00,360 --> 00:14:00,890 All right. 201 00:14:01,140 --> 00:14:06,680 And each of these cells, each of these single items would have this action applied on them. 202 00:14:06,960 --> 00:14:12,870 And then after we go to the print, that one after it will be executed. 203 00:14:12,990 --> 00:14:13,360 All right. 204 00:14:13,710 --> 00:14:16,440 So as you can see, there is only three items here. 205 00:14:16,440 --> 00:14:20,400 So you can say to yourself, what's the difference if I do it? 206 00:14:21,880 --> 00:14:25,030 This way or this way, so we have three lines here. 207 00:14:25,150 --> 00:14:28,250 We have two lines here, maybe three lines here as well. 208 00:14:28,420 --> 00:14:30,010 So what's the difference is the same thing. 209 00:14:30,010 --> 00:14:34,000 But what if I'm having here like a million numbers, right? 210 00:14:35,110 --> 00:14:37,400 Maybe not a million, maybe just 15. 211 00:14:37,810 --> 00:14:38,980 So what happens then? 212 00:14:39,010 --> 00:14:46,450 So if I run it, you can see easily that the values are here multiplied by five automatically. 213 00:14:46,720 --> 00:14:48,910 And I don't need to add any lines. 214 00:14:49,270 --> 00:14:52,900 But on the other hand, if I'm going to add values here. 215 00:14:53,170 --> 00:14:53,560 All right. 216 00:14:53,560 --> 00:14:54,370 In this list. 217 00:14:56,320 --> 00:15:05,450 So each item that I will add here, I will have to add additional additional a line right here, red 218 00:15:05,740 --> 00:15:14,590 cell number three and additional line here so you can see it growing and growing without any control 219 00:15:14,770 --> 00:15:15,590 endlessly. 220 00:15:15,610 --> 00:15:23,800 So if I have like 100 items right here, I have 200 lines right here and it's not efficient. 221 00:15:23,800 --> 00:15:30,450 It's going to be long and spaghetti and you will not see the end of it while here using the follow up. 222 00:15:30,850 --> 00:15:32,600 It's going to stay the same length. 223 00:15:32,620 --> 00:15:34,330 It's going to stay three lines forever. 224 00:15:35,050 --> 00:15:35,490 All right. 225 00:15:35,500 --> 00:15:43,360 So again, what comes after the end is the whole package and what comes after the four is the single 226 00:15:43,360 --> 00:15:46,270 item out of the whole package. 227 00:15:47,140 --> 00:15:52,660 So I think this concludes our introduction video regarding loops. 228 00:15:52,870 --> 00:16:02,070 You can also use the attachment PDF file to this lecture and to use this this slide to read it again. 229 00:16:02,260 --> 00:16:05,020 I think it sums up the introduction pretty well. 230 00:16:05,270 --> 00:16:12,250 I will see you in the next video with deeper explanations regarding for loops and right after we go 231 00:16:12,250 --> 00:16:14,050 over the other loops as well. 232 00:16:14,350 --> 00:16:15,850 I'll see you in the next video. 233 00:16:15,880 --> 00:16:17,010 Thank you and bye bye.