1 00:00:00,000 --> 00:00:01,900 Friends, here 2 00:00:01,900 --> 00:00:04,099 we are going to discuss about loops. 3 00:00:04,900 --> 00:00:10,800 First of all loops are useful to repeat some blocks, to execute 4 00:00:10,800 --> 00:00:16,200 a block of code many times. Why we need to execute a block 5 00:00:16,200 --> 00:00:19,900 of code many times? Block of code is nothing but maybe single 6 00:00:19,900 --> 00:00:21,400 line or multiple lines. 7 00:00:22,400 --> 00:00:26,600 But why we need to execute again and again this block of 8 00:00:26,600 --> 00:00:29,200 code? What is the need, right? 9 00:00:29,500 --> 00:00:32,500 Let me explain this with the first one simple example. 10 00:00:33,600 --> 00:00:37,100 Right. See I am going to save a program named called 11 00:00:37,100 --> 00:00:39,300 'introduction_to_loops.py', and here 12 00:00:39,300 --> 00:00:42,600 what I am doing is, I am taking some list, suppose assume that I 13 00:00:42,600 --> 00:00:48,000 have a list of some values. Now here actually, I have a small 14 00:00:48,000 --> 00:00:51,600 requirement. That requirement is, I need to print the values 15 00:00:51,600 --> 00:00:54,800 of your list one by one, not at a time all your entire list. 16 00:00:54,800 --> 00:00:57,900 I mean, I don't want to print your values 17 00:00:59,500 --> 00:01:02,100 in this way. You are getting output as a list. 18 00:01:02,400 --> 00:01:03,900 No, I don't want to print in that way. 19 00:01:03,900 --> 00:01:08,600 I want to print first 4, then 5, then 6. Then what you are doing? 20 00:01:10,200 --> 00:01:12,700 To get that, first you have to take first value from your 21 00:01:12,700 --> 00:01:13,800 list. With index 22 00:01:13,800 --> 00:01:15,200 I am taking first value. 23 00:01:16,000 --> 00:01:17,900 Then I want to take second value, 24 00:01:17,900 --> 00:01:19,000 then I want to print it. 25 00:01:19,200 --> 00:01:23,300 So taking second value is nothing but 'my_list[1]'. Then 26 00:01:23,300 --> 00:01:27,000 you're printing. Then take third value and then print it. 27 00:01:27,000 --> 00:01:29,400 [no audio] 28 00:01:29,400 --> 00:01:32,100 See the output. Yes, you are getting one by one value. 29 00:01:33,400 --> 00:01:36,900 Okay, for this list suppose you are having three 'print' statements. 30 00:01:36,900 --> 00:01:38,600 Just assume that in your list 31 00:01:38,600 --> 00:01:41,800 you have some this many number of values. 32 00:01:43,500 --> 00:01:46,800 Now, I need to write how many number of 'print' statements? 33 00:01:48,500 --> 00:01:51,200 How many values you have in your list that many number of 34 00:01:51,200 --> 00:01:55,100 'print' statements I need to write. So finally, what is the purpose to write 35 00:01:55,100 --> 00:01:58,700 your 'print' statement that many number of times? Simply you're 36 00:01:58,700 --> 00:02:02,500 taking one by one value from your list and you're printing 37 00:02:02,500 --> 00:02:07,300 that. Taking second value, print; third value' fourth value; fifth value 38 00:02:07,300 --> 00:02:11,900 Likewise, you are taking one by one and your printing it. 39 00:02:13,000 --> 00:02:16,300 So if you have more number of values in your list, you 40 00:02:16,300 --> 00:02:18,500 need to write more number of 'print' statements. 41 00:02:18,900 --> 00:02:22,900 I don't want to write in that way. Because Python is providing, 42 00:02:23,500 --> 00:02:25,200 not only Python in all languages 43 00:02:25,200 --> 00:02:26,300 we have this concept. 44 00:02:26,700 --> 00:02:29,700 Anyway, Python is providing a simple logic. 45 00:02:31,600 --> 00:02:34,600 Just to take one by one value from your list 46 00:02:36,100 --> 00:02:39,600 just to take one by one value from your list. How you can 47 00:02:39,600 --> 00:02:43,100 take one by one value from your list automatically, right? 48 00:02:43,200 --> 00:02:47,900 See that, as of now for your understanding purpose, directly 49 00:02:47,900 --> 00:02:51,800 I am writing syntax called 'for each_value in', your list. 50 00:02:52,300 --> 00:02:55,200 So you can write your variable here or you can also write 51 00:02:55,200 --> 00:02:59,200 directly your list. Both will be fine. Okay. For time being I am writing 52 00:02:59,200 --> 00:03:01,200 directly your variable, sorry your values. 53 00:03:02,200 --> 00:03:05,500 Then after that you have to write ':'. Then 'Enter'. 54 00:03:07,000 --> 00:03:09,800 First of all, what is your health, 55 00:03:09,800 --> 00:03:16,300 what is the action of your 'for' here? Means, because of this 56 00:03:16,300 --> 00:03:21,800 'for', what it will do means, it will take one by one value from your list. 57 00:03:22,800 --> 00:03:26,400 First it will take 4, number 4, and it will store automatically into 58 00:03:26,400 --> 00:03:29,900 your variable called 'each' variable. After that, in case if 59 00:03:29,900 --> 00:03:34,300 you write any number of lines inside of your 'for', these lines 60 00:03:34,300 --> 00:03:35,500 will execute once. 61 00:03:36,600 --> 00:03:39,600 Then after execution of this part once, your Python will 62 00:03:39,600 --> 00:03:42,900 automatically take second value and this value will go and 63 00:03:42,900 --> 00:03:46,600 store into that. Then whatever the lines you have under 64 00:03:46,600 --> 00:03:49,100 your 'for', those lines will execute. 65 00:03:49,700 --> 00:03:53,000 So as of now, my intention is I want to print all the values. 66 00:03:53,600 --> 00:03:56,800 Right. That's why what I am doing is simply 'print(each_value)'. 67 00:03:56,800 --> 00:03:58,700 [no audio] 68 00:03:58,700 --> 00:04:02,500 So guys this 'print' is nothing but, just you are printing your 69 00:04:02,800 --> 00:04:07,200 'each_value' variable, 'each_value' a variable. In this you have 70 00:04:07,200 --> 00:04:10,800 some value. And I am writing this 'print' statement under your 71 00:04:10,800 --> 00:04:13,900 for loop, under your for loop. 72 00:04:14,900 --> 00:04:18,000 So because of this what will happen means, observe that. Whenever 73 00:04:18,000 --> 00:04:21,700 if you run your Python script assume that there is no first 74 00:04:21,700 --> 00:04:24,000 line. As of now assume that there is no first line. 75 00:04:24,300 --> 00:04:28,300 Okay. Then whenever there is a keyword at very 76 00:04:28,500 --> 00:04:32,800 first in a given line, Python will immediately go and look 77 00:04:32,800 --> 00:04:34,300 after 'in'. Whatever 78 00:04:34,300 --> 00:04:35,500 it is there first 79 00:04:35,500 --> 00:04:36,900 your Python will observe this. 80 00:04:37,600 --> 00:04:38,600 Yes, it is a list. 81 00:04:38,600 --> 00:04:43,000 That's why, in list whatever the value we have, first value, 82 00:04:43,000 --> 00:04:45,500 that value will automatically store into that. 83 00:04:45,500 --> 00:04:47,400 [no audio] 84 00:04:47,400 --> 00:04:52,700 Now very first time your 'each_value', 'each_value' variable is 85 00:04:52,700 --> 00:04:56,500 number 4, and after taking that whatever the number of 86 00:04:56,500 --> 00:05:00,700 lines you have inside of your for, those lines will execute 87 00:05:00,700 --> 00:05:04,700 once. But as of now I'm taking only one line. So while executing 88 00:05:04,700 --> 00:05:08,100 that line what I am doing? I am printing the variable of 89 00:05:08,100 --> 00:05:10,100 'each_value', whatever you have. 90 00:05:11,400 --> 00:05:14,900 in this. So that what you will get? You will get number 4. 91 00:05:16,300 --> 00:05:21,100 Right. Then after printing, after the execution of this line, 92 00:05:21,400 --> 00:05:24,700 your Python will automatically take second value, and this 93 00:05:24,700 --> 00:05:28,800 value will store into this variable. After storing your second 94 00:05:28,800 --> 00:05:30,400 value into this variable, 95 00:05:30,800 --> 00:05:33,800 this block will execute once again, but while executing what 96 00:05:33,800 --> 00:05:36,700 we are doing? This variable value we are printing. That's 97 00:05:36,700 --> 00:05:39,300 why you're going to get second value. Likewise, 98 00:05:39,300 --> 00:05:43,600 your Python will take one by one value from here and that 99 00:05:43,600 --> 00:05:46,200 value will store into this variable 100 00:05:46,200 --> 00:05:48,200 first. After that only this 101 00:05:49,900 --> 00:05:51,000 block will execute. 102 00:05:52,000 --> 00:05:54,500 So because of that see what it is doing. 103 00:05:56,500 --> 00:05:59,200 You're getting all values one by one after running your code. 104 00:06:00,600 --> 00:06:03,800 So now, guys actually what you are doing, 105 00:06:03,800 --> 00:06:07,600 I mean your Python? Taking first value, storing into that, executing 106 00:06:07,600 --> 00:06:11,600 this block, then taking second value, storing into this variable, 107 00:06:11,600 --> 00:06:16,200 then executing this block, taking third value, storing 108 00:06:16,200 --> 00:06:19,800 into this variable, and then executing this block. You're repeating 109 00:06:19,800 --> 00:06:20,800 here itself, right? 110 00:06:20,800 --> 00:06:24,300 That's why this is like a loop, and that's why this is called 111 00:06:24,300 --> 00:06:31,300 loop. And one more thing, executing your block of code, right, 112 00:06:31,700 --> 00:06:35,200 with the help of your loop, how many number of times? How many 113 00:06:35,200 --> 00:06:38,600 number of values you have inside of a list, that many number 114 00:06:38,600 --> 00:06:42,400 of times you are repeating, and each repetition is called 115 00:06:42,500 --> 00:06:45,200 iteration. Iteration. 116 00:06:45,200 --> 00:06:48,900 [no audio] 117 00:06:48,900 --> 00:06:51,100 Right. So suppose assume that in 'my_list' 118 00:06:51,100 --> 00:06:54,000 I have four values. Just assume that. Then you are going to 119 00:06:54,000 --> 00:06:58,000 iterate, iterate, this logic, this line, you're going to repeat 120 00:06:58,000 --> 00:07:02,100 this line 4 number of times, or 4 iterations will 121 00:07:02,100 --> 00:07:04,300 happen if you have 4 values. 122 00:07:05,900 --> 00:07:08,000 Not only this, simply your variable, see 123 00:07:08,000 --> 00:07:12,300 I can also do something like 'print' in this way. 124 00:07:12,300 --> 00:07:13,400 Just I'm doing this one. 125 00:07:13,500 --> 00:07:15,400 Let me write three 'print' statements, 126 00:07:16,700 --> 00:07:18,700 not only 'print' statements, you can write anything. 127 00:07:18,900 --> 00:07:21,000 But as of now just for our understanding purpose 128 00:07:21,000 --> 00:07:22,200 I am writing simple lines. 129 00:07:22,700 --> 00:07:24,500 Now, let me remove this list. 130 00:07:25,400 --> 00:07:27,700 Okay. Very first line itself our loop. 131 00:07:27,700 --> 00:07:29,300 I don't want to write this many number of values, 132 00:07:29,300 --> 00:07:31,100 let me write, suppose three values, 133 00:07:31,400 --> 00:07:34,000 same action will be repeated for all values, in case if you 134 00:07:34,000 --> 00:07:39,400 have any number of values. Now what will happen? First thing, 135 00:07:39,500 --> 00:07:42,500 whenever if you are executing, your Python will look very 136 00:07:42,500 --> 00:07:46,000 first word, right. If it is 'for' then 'for' is a loop, 137 00:07:46,000 --> 00:07:49,200 that's why your python will immediately look in the same 138 00:07:49,200 --> 00:07:51,900 line after 'in' whatever you have, 139 00:07:51,900 --> 00:07:53,800 your Python will look that place first. 140 00:07:55,000 --> 00:07:57,700 Then what you are having here? List, which consists of 141 00:07:57,700 --> 00:08:02,000 three values. Then Python will immediately take first value, and 142 00:08:02,000 --> 00:08:06,000 this value will store into this variable. After storing that 143 00:08:06,300 --> 00:08:10,100 the number of lines which you have inside of your loop, those 144 00:08:10,100 --> 00:08:14,100 lines will execute once. While executing, in case if you want 145 00:08:14,100 --> 00:08:17,300 to use this variable you can use it, otherwise no problem. 146 00:08:18,100 --> 00:08:21,100 As of now three 'print' statements. First 'print' statement, simply 147 00:08:21,100 --> 00:08:24,200 some string you're printing. Second 'print' statement, you are 148 00:08:24,200 --> 00:08:27,000 printing some variable value. That value is nothing but the value 149 00:08:27,000 --> 00:08:30,100 which is there currently in this variable. Then after that 150 00:08:30,100 --> 00:08:31,300 again you are printing this. 151 00:08:32,700 --> 00:08:35,600 Right. Now, let me write a different symbol 152 00:08:35,600 --> 00:08:36,900 so that it will be clear for you. 153 00:08:36,900 --> 00:08:39,600 [no audio] 154 00:08:39,600 --> 00:08:41,000 Now, let me run it and see the output. 155 00:08:41,000 --> 00:08:43,200 [no audio] 156 00:08:43,200 --> 00:08:47,600 First time, second time, third time; you are repeating your 157 00:08:47,600 --> 00:08:48,700 logic three times. 158 00:08:48,700 --> 00:08:51,400 This is for first time, this is for second time, 159 00:08:51,500 --> 00:08:55,500 and this is for third time. While repeating, while executing 160 00:08:55,500 --> 00:09:01,800 this first time, 'value', this variable is 4, and we are executing 161 00:09:01,800 --> 00:09:04,700 that. That's why you are getting output as a 4. Now, 162 00:09:04,700 --> 00:09:08,200 I don't want to use this 'each' variable inside of my loop. 163 00:09:08,200 --> 00:09:09,500 Yes, no need to use that. 164 00:09:09,500 --> 00:09:12,000 It's not mandatory. Based on requirement we will use it. 165 00:09:12,300 --> 00:09:13,400 I am writing simply, 166 00:09:14,900 --> 00:09:19,300 "This is a iteration". Any message also I can write it. 167 00:09:19,300 --> 00:09:21,800 [no audio] 168 00:09:21,800 --> 00:09:24,400 Let me save it and observe the output. I am running. 169 00:09:25,900 --> 00:09:28,300 While executing first time, three 'print' statements, 170 00:09:28,300 --> 00:09:32,100 "--This is a iteration", and then some line. 171 00:09:33,400 --> 00:09:37,900 That is for 'each' value 4. Then second time, 5 value, with 172 00:09:37,900 --> 00:09:40,900 5 you are repeating this, six you're repeating that, 173 00:09:40,900 --> 00:09:42,200 that's why you are getting three times. 174 00:09:42,700 --> 00:09:45,600 So guys here, not only take, I mean you can take any variable. 175 00:09:45,600 --> 00:09:46,600 Not only 'each_value', 176 00:09:46,600 --> 00:09:49,300 I can take simply 'x' also. No problem. See the output. 177 00:09:49,300 --> 00:09:51,100 [no audio] 178 00:09:51,100 --> 00:09:55,500 That's it. Okay. So this is called simply your for loop. 179 00:09:56,300 --> 00:10:01,000 So finally you have to understand that this for loop is useful 180 00:10:01,000 --> 00:10:05,000 to repeat some block of code or some set of lines 181 00:10:06,000 --> 00:10:09,100 based on your requirement how many times you want to repeat. 182 00:10:09,700 --> 00:10:13,300 So that is decided by the value which you are providing here. 183 00:10:15,100 --> 00:10:16,500 Not only list, you can take some other things as well. 184 00:10:16,500 --> 00:10:19,500 I will show you that. Right. Fine. 185 00:10:19,900 --> 00:10:23,400 So why loops are useful guys? 186 00:10:25,000 --> 00:10:27,800 So in all programming languages we have a requirement to 187 00:10:27,800 --> 00:10:32,200 execute some block of code number of times, many times. 188 00:10:32,500 --> 00:10:35,900 So that's why we are using loops concepts. Now in Python 189 00:10:35,900 --> 00:10:38,900 we have two types of loops - for loop and while loop. 190 00:10:38,900 --> 00:10:40,800 [no audio] 191 00:10:40,800 --> 00:10:46,900 Okay. Now, for loop. For loop in Python is used to iterate or execute 192 00:10:46,900 --> 00:10:52,000 many number of times over a sequence. That sequence maybe 193 00:10:52,200 --> 00:10:57,000 a list or tuple or string or any other iterable objects. 194 00:10:57,900 --> 00:11:01,000 As of now, don't worry about iterable objects. 195 00:11:01,000 --> 00:11:02,800 [no audio] 196 00:11:02,800 --> 00:11:07,400 You are going to repeat some block of code using loop, and 197 00:11:07,400 --> 00:11:10,700 while repeating, see previously we used somewhere here list, 198 00:11:10,700 --> 00:11:12,400 right. Not only list, 199 00:11:12,400 --> 00:11:16,800 you can also provide a list, tuple, or a string. 200 00:11:17,400 --> 00:11:18,300 Let me show you that. 201 00:11:18,300 --> 00:11:20,100 [no audio] 202 00:11:20,100 --> 00:11:21,500 This is using list, right? 203 00:11:22,000 --> 00:11:25,700 I can also use in your loop a tuple also, no problem. 204 00:11:25,000 --> 00:11:28,200 [no audio] 205 00:11:28,200 --> 00:11:30,400 Let me do that now with the help of tuple. 206 00:11:31,700 --> 00:11:36,300 'for value in', this is a variable guys, 207 00:11:36,300 --> 00:11:40,300 you can take anything based on your requirement, in your tuple, 208 00:11:40,300 --> 00:11:46,500 let me write directly my tuple. '(4,5,6', then ', "hi")'. 209 00:11:46,900 --> 00:11:48,900 Now how many values are there in your tuple? 210 00:11:49,900 --> 00:11:51,800 Four values. This is a string, 211 00:11:51,800 --> 00:11:55,300 but this string is one value for your tuple. Four values. 212 00:11:56,300 --> 00:11:57,400 Right. Now, simply 213 00:11:57,400 --> 00:12:00,400 I am writing, 'print("hi")', or simply 214 00:12:00,400 --> 00:12:06,300 I'm writing this "=". Then because of your loop, for loop 215 00:12:06,300 --> 00:12:09,500 you are repeating this line, you're printing this line, 216 00:12:09,600 --> 00:12:10,900 you are repeating this line. 217 00:12:11,800 --> 00:12:13,500 How many values you have? Four values. 218 00:12:13,500 --> 00:12:16,800 So four times you are going to execute this line again and again 219 00:12:16,800 --> 00:12:20,700 immediately. Now see the output. Four times you are getting. 220 00:12:21,400 --> 00:12:25,500 But while executing the block which is there under your loop 221 00:12:25,500 --> 00:12:30,000 at that time in case if you need a variable value of, 'value' 222 00:12:30,200 --> 00:12:33,700 you can use it. Now see that. That's it. 223 00:12:35,300 --> 00:12:38,300 So whether you want to use your variable, whatever you have 224 00:12:38,300 --> 00:12:41,200 here, that variable you can use it or you can skip it. 225 00:12:42,600 --> 00:12:45,600 Right. Now, let me give one simple example. 226 00:12:45,600 --> 00:12:48,200 [no audio] 227 00:12:48,200 --> 00:12:51,400 Assume that. Yeah, before going to that example 228 00:12:51,500 --> 00:12:54,300 I will take one more thing, string. As of now 229 00:12:54,300 --> 00:12:56,700 we worked with list, tuple, then let me take string. 230 00:12:58,100 --> 00:12:59,700 After that, I will go with one example. 231 00:13:01,200 --> 00:13:08,500 For, let me take, yeah, 'for each_char in', let me take 'python scripting', 232 00:13:09,300 --> 00:13:12,300 space is also a string, right, then ':'. 233 00:13:12,500 --> 00:13:16,300 Then what I am doing is, 'print(each_char)', 234 00:13:16,300 --> 00:13:18,400 or if you don't want to do that, 235 00:13:18,400 --> 00:13:20,900 [no audio] 236 00:13:20,900 --> 00:13:23,500 let me simply do simply "python". 237 00:13:23,900 --> 00:13:28,000 Okay. Then simply print, I am printing hyphens. 238 00:13:28,600 --> 00:13:33,900 So now you are going to repeat this line based on how many 239 00:13:33,900 --> 00:13:35,700 characters are there in a given string. 240 00:13:36,800 --> 00:13:39,600 So how it is going to repeat means, immediately, whenever if 241 00:13:39,600 --> 00:13:43,100 you run your Python script, because this is a loop, 'for', your 242 00:13:43,100 --> 00:13:45,000 Python will take first character from here. 243 00:13:45,000 --> 00:13:48,300 If it is a list it will take first value from your list, or 244 00:13:48,300 --> 00:13:50,900 if it is a tuple it will take first value from your tuple. 245 00:13:50,900 --> 00:13:54,800 But now this is a string, so it will take first character 246 00:13:54,800 --> 00:13:58,400 automatically and that character it will store into this variable. 247 00:13:59,300 --> 00:14:03,100 After storing that, whatever the logic, whatever the block, 248 00:14:03,100 --> 00:14:05,900 whatever the lines you have under your loop, those lines will 249 00:14:05,900 --> 00:14:09,300 execute once. While executing if you want to 250 00:14:09,300 --> 00:14:11,000 use this variable you can use it, 251 00:14:11,400 --> 00:14:14,500 otherwise you can skip it. As of now, I'm not using. Simply 252 00:14:14,500 --> 00:14:17,400 what I am doing? See the output, you are getting hyphens. 253 00:14:17,400 --> 00:14:20,000 How many times? Six times, because you have six characters. 254 00:14:20,600 --> 00:14:23,000 Now what I am doing is, along with your hyphens, 255 00:14:23,200 --> 00:14:27,200 I'm also going to print the characters which is stored in 256 00:14:27,200 --> 00:14:30,300 your 'each_char' variable. Now see the result. 257 00:14:31,400 --> 00:14:36,700 'p-y-t-h-o-n'. See, first time your Python is taking letter 'p', character 258 00:14:36,700 --> 00:14:41,000 'p', and it is storing into this variable. After storing into this variable, 259 00:14:41,800 --> 00:14:45,200 this line will execute once, first iteration. 260 00:14:45,200 --> 00:14:48,100 This is called first iteration. Then because of that you are getting 261 00:14:48,100 --> 00:14:52,200 '--', and then 'p'. Then Python will immediately go and take next 262 00:14:52,200 --> 00:14:56,200 character 'y'. Then 'y' is going to store into this variable. After 263 00:14:56,200 --> 00:15:00,600 that your Python will execute this line once. So while executing 264 00:15:00,600 --> 00:15:03,700 now you're printing this variable. Currently the value which 265 00:15:03,700 --> 00:15:06,700 is there in your 'each_char' variable is 'y', that's why 266 00:15:06,700 --> 00:15:10,400 you're getting 'y'. Likewise it should go to up to 'n'. Right. 267 00:15:10,400 --> 00:15:12,600 Assume that you just now printed 'n', 268 00:15:12,600 --> 00:15:15,400 I mean with the help of your loop, after that again your 269 00:15:15,400 --> 00:15:18,800 Python will try to get next character from your string but 270 00:15:18,900 --> 00:15:21,300 you don't have anything. All characters are over. 271 00:15:21,600 --> 00:15:24,800 That's why your Python will come back, come out from your 272 00:15:24,800 --> 00:15:28,900 loop, and after loop if you have any lines, those lines will execute. 273 00:15:30,500 --> 00:15:32,900 Right. That's it. 274 00:15:33,100 --> 00:15:35,300 So guys, 'print' statement, 275 00:15:35,300 --> 00:15:38,000 I want to repeat. That's why that line should be there under 276 00:15:38,000 --> 00:15:39,000 this for loop. 277 00:15:39,500 --> 00:15:42,700 That's why I am providing some space. Suppose 278 00:15:42,700 --> 00:15:45,100 if I write one more line, 'print()' 279 00:15:45,100 --> 00:15:47,300 [no audio] 280 00:15:47,300 --> 00:15:53,200 "we are working with loops". Now see that. This line will execute 281 00:15:53,200 --> 00:15:58,500 only once. When it is going to execute that one time is after 282 00:15:58,500 --> 00:16:00,300 complete execution of 283 00:16:00,500 --> 00:16:03,300 your loop. Because this 'print' statement is outside of your 284 00:16:03,300 --> 00:16:06,300 loop, there is no relation between this line and your for 285 00:16:06,300 --> 00:16:10,800 loop. So in case if you have a relation between this for 286 00:16:10,800 --> 00:16:14,200 loop and this line, this line should be there inside of your 287 00:16:14,200 --> 00:16:17,500 loop. If you want to write this last line inside of your 288 00:16:17,500 --> 00:16:20,900 loop, you have to provide some space, and the lines which 289 00:16:20,900 --> 00:16:23,600 are there under your loop should have same space for each 290 00:16:23,600 --> 00:16:25,300 and every line. First 291 00:16:25,300 --> 00:16:26,600 let me run it and see the output. 292 00:16:26,600 --> 00:16:28,600 [no audio] 293 00:16:28,600 --> 00:16:29,600 See this last line you're 294 00:16:29,600 --> 00:16:32,500 getting only once because that is not a part of your 295 00:16:32,500 --> 00:16:35,700 loop. It is outside of your loop. Only the lines which are 296 00:16:35,700 --> 00:16:40,300 there under your loop, those lines will execute, right. Now 297 00:16:40,300 --> 00:16:42,300 I want to write that line also inside of my loop 298 00:16:42,300 --> 00:16:46,100 then write for all your lines same space, 299 00:16:47,500 --> 00:16:50,600 right, so that these two lines are there now under your loop. 300 00:16:51,000 --> 00:16:55,300 Now your loop will repeat these two lines based on how 301 00:16:55,300 --> 00:16:57,100 many characters you have in your string. 302 00:16:57,300 --> 00:17:01,000 So six characters, now these two lines are going to repeat 303 00:17:01,000 --> 00:17:04,400 six times. See that. That's it. 304 00:17:04,400 --> 00:17:07,200 [no audio] 305 00:17:07,200 --> 00:17:09,000 Right. Fine. 306 00:17:10,800 --> 00:17:13,400 Now, let me go with one simple example as of now. 307 00:17:13,400 --> 00:17:16,800 [no audio] 308 00:17:16,800 --> 00:17:17,800 Assume that 309 00:17:18,500 --> 00:17:22,000 I have a list which consists of some values. 310 00:17:22,000 --> 00:17:24,200 Otherwise, you can also read your list from your command 311 00:17:24,200 --> 00:17:26,500 line, right. Using your 'eval(input( 312 00:17:26,500 --> 00:17:27,700 "Enter your list of values: "))' 313 00:17:27,700 --> 00:17:28,900 you can read it. As of now 314 00:17:28,900 --> 00:17:30,000 I am taking 'my_list'. 315 00:17:30,599 --> 00:17:32,900 I don't know what are the values there in 'my_list', 316 00:17:32,900 --> 00:17:35,900 [no audio] 317 00:17:35,900 --> 00:17:37,500 okay. Maybe any number of values. 318 00:17:38,700 --> 00:17:40,099 Now my requirement is, 319 00:17:40,099 --> 00:17:42,200 [no audio] 320 00:17:42,200 --> 00:17:43,200 I want to 321 00:17:44,800 --> 00:17:47,000 print all the values from 322 00:17:47,000 --> 00:17:48,700 your loop, from your list. 323 00:17:49,400 --> 00:17:55,200 Okay, first let me print that. 'for each in my_list', then 324 00:17:55,200 --> 00:17:57,500 let me 'print(each)'. 325 00:17:57,500 --> 00:17:59,900 [no audio] 326 00:17:59,900 --> 00:18:01,600 Yes, I'm printing. Now 327 00:18:01,600 --> 00:18:06,000 I'm able to get one by one value. But before printing I want 328 00:18:06,000 --> 00:18:10,200 to decide whether it is a even number or odd number. If it 329 00:18:10,200 --> 00:18:13,900 is even number I want to print your number, then "even". If it 330 00:18:13,900 --> 00:18:18,100 is odd number, I want to print your number and then "odd". Right. 331 00:18:18,100 --> 00:18:22,100 So that's why what I am doing is, before printing I am finding 332 00:18:22,100 --> 00:18:25,700 remainder, remainder with two. So 'each 333 00:18:26,500 --> 00:18:32,500 %2', and if remainder value, if it is equal to 0, 334 00:18:32,500 --> 00:18:34,100 I can say that that is an even number. 335 00:18:35,700 --> 00:18:39,500 Now guys, this 'print' is under your 'if'. Be clear. 336 00:18:40,000 --> 00:18:42,900 If this condition is satisfied then only I want to print this line, 337 00:18:42,900 --> 00:18:47,200 that's why I'm writing this line under your 'if' condition. 338 00:18:47,500 --> 00:18:53,600 So if it is 0 then I want to write '--'. Let me write 'f' string, that's better. 339 00:18:55,100 --> 00:19:01,100 So whatever the variable you have, " is even". That's it. 340 00:19:02,900 --> 00:19:07,200 'else', 'else' means remainder is not equal to 0, then I can say 341 00:19:07,600 --> 00:19:09,700 your number, then 342 00:19:11,200 --> 00:19:15,100 "is odd". Guys, simple example, but it is useful. 343 00:19:16,000 --> 00:19:19,300 Why means, I am writing number of lines inside of your loop, 344 00:19:19,700 --> 00:19:22,500 now all these lines are going to repeat based on how many 345 00:19:22,500 --> 00:19:24,800 values you have in your list. As of now 346 00:19:24,800 --> 00:19:29,500 this is a list. It may be a list, or string, or tuple, for time being 347 00:19:31,000 --> 00:19:35,400 Now run it and see the output. 3 is odd, 4 is even, 34 is even. Yes, 348 00:19:35,400 --> 00:19:40,400 it's working perfectly. Right. Guys, try to understand this 349 00:19:40,500 --> 00:19:44,000 loop concept by practicing again 350 00:19:44,000 --> 00:19:48,900 and again same code. See while writing you have to think what 351 00:19:48,900 --> 00:19:51,500 we are doing, what is the step 352 00:19:51,500 --> 00:19:53,800 your loop is performing, right? 353 00:19:53,800 --> 00:19:57,300 If you are able to think about it, then you can easily understand your loop. 354 00:19:58,100 --> 00:20:00,700 See in real time loops are very, very important. 355 00:20:01,900 --> 00:20:03,800 Right. Suppose assume that you are going to write some hundred 356 00:20:03,800 --> 00:20:05,300 lines of some script. 357 00:20:06,000 --> 00:20:08,500 So if you are going to write some 100 lines or 200 lines 358 00:20:08,500 --> 00:20:10,300 of Python script, then definitely you are 359 00:20:10,300 --> 00:20:12,200 going to use 'if' conditions and loops, 360 00:20:13,800 --> 00:20:15,500 max. Okay. 361 00:20:16,800 --> 00:20:18,900 Okay, guys, thank you for watching this video. 362 00:20:19,200 --> 00:20:21,800 And one more thing guys, we have while loop as well, but this 363 00:20:21,800 --> 00:20:22,800 we'll discuss later. 364 00:20:23,300 --> 00:20:26,600 So actually while loop is used to execute a block of statements 365 00:20:26,600 --> 00:20:30,100 repeatedly until a given condition is satisfied. As of now 366 00:20:30,100 --> 00:20:31,500 don't go with your while loop, 367 00:20:31,700 --> 00:20:33,500 just think about loop, for loop. 368 00:20:34,300 --> 00:20:36,800 Okay. Anyway I will discuss about your while loop later. 369 00:20:38,700 --> 00:20:40,100 Okay. Thank you. Bye. 370 00:20:40,100 --> 00:20:45,120 [no audio]