1 00:00:00,000 --> 00:00:03,900 Friends, we know that loops are useful to repeat or iterate 2 00:00:03,900 --> 00:00:07,100 a logic and already we have seen for loop. 3 00:00:07,700 --> 00:00:10,800 Now we have one more loop with your Python that is called while loop. 4 00:00:12,200 --> 00:00:16,500 Let me explain about your while loop by taking simple example. 5 00:00:16,500 --> 00:00:19,800 First of all, I am taking simply 'print( 6 00:00:22,100 --> 00:00:23,600 ("welcome to loops")'. 7 00:00:23,600 --> 00:00:27,500 Suppose I want to print this message only one time, then I can 8 00:00:27,500 --> 00:00:29,500 directly write 'print ("Welcome to loops")'. 9 00:00:30,700 --> 00:00:34,100 But I want to print this message 10 times, then what I can do? 10 00:00:35,100 --> 00:00:37,800 One thing is, I can write this statement 10 times, 11 00:00:37,800 --> 00:00:39,100 'print ("Welcome to loops")', 12 00:00:39,100 --> 00:00:43,000 "Welcome to loops", likewise, 10 times I can write. But already 13 00:00:43,000 --> 00:00:46,000 we know for loop, that's why what I am doing is, I am going 14 00:00:46,000 --> 00:00:50,100 to iterate. I am going to iterate this logic for 10 times 15 00:00:50,700 --> 00:00:52,900 'for each in range(10)'. 16 00:00:53,400 --> 00:00:56,200 Not only 'range(10)', directly you can provide a string which 17 00:00:56,200 --> 00:01:01,100 consists of 10 characters, such that once if you run your code 18 00:01:01,100 --> 00:01:05,200 it is going to repeat your, it is going to print your message 10 times 19 00:01:06,599 --> 00:01:10,600 But I want to print this message infinite number of times. 20 00:01:10,600 --> 00:01:13,400 [no audio] 21 00:01:13,400 --> 00:01:18,500 I want to repeat this message for infinite number of times. 22 00:01:19,600 --> 00:01:25,300 Let me modify this for loop with, 'while True'. Now see the 23 00:01:25,300 --> 00:01:28,100 result. So to get more clarity 24 00:01:28,100 --> 00:01:30,800 I'm writing one more line here so that you can understand 25 00:01:30,900 --> 00:01:33,200 whether it is going to repeat it infinite number of times or 26 00:01:33,200 --> 00:01:34,200 not. See that. 27 00:01:35,200 --> 00:01:38,100 It is going to repeat for infinite number of times. 28 00:01:39,300 --> 00:01:43,500 Right. So sometimes you may need to repeat your logic for infinite 29 00:01:43,500 --> 00:01:44,500 number of times. 30 00:01:45,300 --> 00:01:49,100 Let me give you a very simple example. First let me stop it. 31 00:01:52,100 --> 00:01:54,700 So if you write 'False', then it will stop. 32 00:01:54,700 --> 00:01:56,500 [no audio] 33 00:01:56,500 --> 00:01:59,400 That's fine. So first of all, you have to understand that if you want 34 00:01:59,400 --> 00:02:01,900 to repeat a logic for infinite number of times, then you 35 00:02:01,900 --> 00:02:03,300 have to go with while loop. 36 00:02:04,800 --> 00:02:07,000 Right. So guys how it is going 37 00:02:07,000 --> 00:02:09,300 to work? Yeah, let me explain that as well. 38 00:02:09,300 --> 00:02:12,000 [no audio] 39 00:02:12,000 --> 00:02:15,100 See whenever if you run your script with this while loop, 40 00:02:15,100 --> 00:02:19,700 because of while, right, while is a loop, 41 00:02:19,900 --> 00:02:23,800 first of all, this logic will repeat. But how many number of times? 42 00:02:25,100 --> 00:02:28,300 If you write directly 'True' here, then it is going to repeat 43 00:02:28,300 --> 00:02:29,400 for infinite number of times. 44 00:02:29,400 --> 00:02:33,300 Same logic will re-execute for infinite number of times 45 00:02:33,300 --> 00:02:37,900 if you write 'True'. How it is doing means, whenever there 46 00:02:37,900 --> 00:02:40,800 is a while, your Python will check first the value here. 47 00:02:40,800 --> 00:02:42,700 It is a True, right, if it is True 48 00:02:42,700 --> 00:02:46,300 it will execute once. Then again, your Python will come back 49 00:02:46,300 --> 00:02:49,100 to this position and again, it will check. 50 00:02:49,100 --> 00:02:53,000 Anyway, this is a fixed value 'True', that's why again it will execute. 51 00:02:53,000 --> 00:02:54,600 Again our Python will come back to here, 52 00:02:55,100 --> 00:02:56,700 again this is 'True'. It will execute. 53 00:02:56,700 --> 00:02:59,700 Likewise it is 'True' for lifetime 54 00:02:59,700 --> 00:03:02,400 so it is going to repeat for infinite number of times. 55 00:03:02,700 --> 00:03:05,900 That's why this is a infinite loop. Right. 56 00:03:05,900 --> 00:03:08,400 [no audio] 57 00:03:08,400 --> 00:03:10,400 That's fine. Now, 58 00:03:10,400 --> 00:03:12,400 [no audio] 59 00:03:12,400 --> 00:03:14,900 what is the use of infinite loop? Let us assume that you 60 00:03:14,900 --> 00:03:18,000 need to monitor a file system for lifetime. 61 00:03:18,000 --> 00:03:19,900 Then what I will do is, simply 'True'. Here, 62 00:03:19,900 --> 00:03:22,500 I will write the logic to monitor your file system. As of 63 00:03:22,500 --> 00:03:26,700 now, simply I am writing 'print' statement, "Monitoring 64 00:03:26,700 --> 00:03:28,900 [no audio] 65 00:03:28,900 --> 00:03:31,100 file system usage", 66 00:03:32,600 --> 00:03:33,700 just for our clarity 67 00:03:33,700 --> 00:03:34,800 I'm writing one more line. 68 00:03:34,800 --> 00:03:37,800 [no audio] 69 00:03:37,800 --> 00:03:38,800 Now see that, 70 00:03:39,600 --> 00:03:42,600 for every 1 minute, I need to monitor my file system usage, 71 00:03:42,600 --> 00:03:46,900 but for lifetime. Then what I will do? Just I'm importing 'time' module. 72 00:03:46,900 --> 00:03:49,100 [no audio] 73 00:03:49,100 --> 00:03:53,000 Then after every 1 minute, right, but instead of 74 00:03:53,000 --> 00:03:55,600 one minute, what I will do is, as of now I am taking simply 75 00:03:55,600 --> 00:04:00,000 one second. Now for every one second now our script will 76 00:04:00,000 --> 00:04:04,000 monitor your file system usage. In case if it has reached the 77 00:04:04,000 --> 00:04:08,800 threshold value, then you can send mail alerts, right. Now see that. 78 00:04:10,100 --> 00:04:12,900 Once if you run your script and if you leave it, it is going 79 00:04:12,900 --> 00:04:17,000 to monitor your file system for lifetime. That's it. 80 00:04:17,700 --> 00:04:20,700 So this is one of the usage, when you want to repeat your 81 00:04:20,700 --> 00:04:25,600 logic for infinite number of times, right? That's fine. 82 00:04:26,800 --> 00:04:31,000 Now sometimes you don't know how many number of times you 83 00:04:31,000 --> 00:04:34,800 need to repeat your logic, but after a certain number of 84 00:04:34,800 --> 00:04:36,800 times you need to stop. 85 00:04:38,400 --> 00:04:39,800 Means just assume that 86 00:04:39,800 --> 00:04:41,800 [no audio] 87 00:04:41,800 --> 00:04:44,800 I want to print a number then next 88 00:04:44,800 --> 00:04:48,100 time I want to print your number by adding some random number. 89 00:04:48,900 --> 00:04:51,400 I mean first let me write logic so that you can understand. 90 00:04:51,800 --> 00:04:53,300 Let me take initially a 'value'. 91 00:04:54,100 --> 00:04:57,200 let's say some number 4. Then I am taking 'while True'. 92 00:04:58,700 --> 00:05:02,900 Then I am printing your 'value', initial value. Then what is 93 00:05:02,900 --> 00:05:06,100 the next value you want to print? I am taking next 'value +', 94 00:05:06,100 --> 00:05:07,600 some random number I am adding. 95 00:05:07,600 --> 00:05:10,300 Now if I run this, see that it is going to get 96 00:05:11,500 --> 00:05:14,500 some numbers. Right. 97 00:05:16,400 --> 00:05:17,400 Let me stop it. 98 00:05:17,400 --> 00:05:21,200 [no audio] 99 00:05:21,200 --> 00:05:23,700 So in this way if you run, you are going to get your numbers 100 00:05:23,700 --> 00:05:24,900 infinite number of times, 101 00:05:24,900 --> 00:05:29,200 I mean infinite numbers you are going to get. Initially 4, then 102 00:05:30,200 --> 00:05:33,700 460, then 4 something, 900 something, like that you are 103 00:05:33,700 --> 00:05:37,600 going to get. But I want to, I don't want to get all the numbers. 104 00:05:38,500 --> 00:05:43,800 I want to get the numbers only suppose 105 00:05:45,400 --> 00:05:47,700 let me write, if your value 106 00:05:48,700 --> 00:05:51,900 is less than or equal to some, this number. 107 00:05:51,900 --> 00:05:55,100 [no audio] 108 00:05:55,100 --> 00:05:56,200 So here what I am writing, 109 00:05:56,200 --> 00:06:00,100 I am writing a condition such that, previously here you have 'True', right? 110 00:06:00,100 --> 00:06:02,800 [no audio] 111 00:06:02,800 --> 00:06:04,300 So now let me write 'True' first. 112 00:06:04,300 --> 00:06:06,800 [no audio] 113 00:06:06,800 --> 00:06:10,000 You're going to get some numbers, but I don't want to get 114 00:06:10,000 --> 00:06:10,900 all these numbers. 115 00:06:10,900 --> 00:06:15,900 I want to stop this loop once if I reach my value more 116 00:06:15,900 --> 00:06:20,800 than this. Now see the result. In case if your 'value' is 117 00:06:20,800 --> 00:06:21,800 greater than 118 00:06:23,100 --> 00:06:25,600 6789, then you are going to stop it. 119 00:06:27,100 --> 00:06:29,700 Actually, I'm repeating this for infinite number of times, but 120 00:06:29,700 --> 00:06:31,600 I don't want to repeat infinite number of times. 121 00:06:32,500 --> 00:06:36,500 I want to get the numbers which are less than this. 122 00:06:35,500 --> 00:06:38,300 [no audio] 123 00:06:38,300 --> 00:06:43,300 So intentionally you can stop your while loop based on condition as well. 124 00:06:43,300 --> 00:06:45,100 [no audio] 125 00:06:45,100 --> 00:06:47,000 Right. Initially this is the value. 126 00:06:47,000 --> 00:06:49,100 I am printing that value. For my 'value' 127 00:06:49,100 --> 00:06:52,100 I'm adding something and I am then printing, right. 128 00:06:53,400 --> 00:06:56,600 Now to get more clarity, let me take simply 129 00:06:56,600 --> 00:07:01,800 [no audio] 130 00:07:01,800 --> 00:07:04,400 'while True', simply print("hello"). 131 00:07:05,400 --> 00:07:06,600 Now it is going to repeat infinite 132 00:07:06,600 --> 00:07:10,200 number of times, but I don't want to print infinite number of times. 133 00:07:11,600 --> 00:07:15,200 Right. I want to print only for suppose four times. 134 00:07:15,200 --> 00:07:18,000 Let me take initially 'cnt=0'. 135 00:07:19,600 --> 00:07:22,400 Now what I am writing is, here I am writing 'cnt' value 136 00:07:22,400 --> 00:07:23,400 if it is 137 00:07:25,400 --> 00:07:28,200 '<=', '<5', 138 00:07:28,200 --> 00:07:30,700 [no audio] 139 00:07:30,700 --> 00:07:32,700 or you can take even maybe '<= 5'. 140 00:07:32,700 --> 00:07:34,400 Let me take initially 'cnt = 1'. 141 00:07:34,400 --> 00:07:38,100 [no audio] 142 00:07:38,100 --> 00:07:41,400 Right. Now what I am doing is I am increasing count value as well. 143 00:07:41,400 --> 00:07:43,400 [no audio] 144 00:07:43,400 --> 00:07:46,800 Now see that you're going to get your output only four five 145 00:07:46,800 --> 00:07:49,500 times, you are going to repeat this logic only for five times. 146 00:07:49,600 --> 00:07:51,800 Then how it is working? Nothing 147 00:07:51,800 --> 00:07:55,200 is there. Simply first you are initializing some condition. 148 00:07:55,200 --> 00:07:58,700 Sorry, I'm sorry some variable, initializing, 'cnt = 1'. 149 00:07:59,200 --> 00:08:02,900 So while, first of all while means, you have to assume that 150 00:08:03,000 --> 00:08:05,600 the lines which are there under your while loop will repeat 151 00:08:05,600 --> 00:08:08,500 for infinite number of times, right? 152 00:08:08,600 --> 00:08:09,700 So how it is going to work? 153 00:08:09,700 --> 00:08:13,800 We know that. 'while' first Python will check this condition 154 00:08:13,800 --> 00:08:17,600 result, 'cnt' is initially 1, right. 1 less than equals to 5 155 00:08:17,600 --> 00:08:19,500 means 'True'. If it is 'True', 156 00:08:19,500 --> 00:08:21,700 your Python will execute these two lines once. 157 00:08:22,200 --> 00:08:25,800 So because of this you're also increasing your 'cnt' value. 158 00:08:25,800 --> 00:08:30,200 Initially it is 1, now after executing first time, for first 159 00:08:30,200 --> 00:08:35,600 iteration your 'cnt' value is becoming 1 + 1, w. Now with 160 00:08:35,600 --> 00:08:39,799 this 2, now 'cnt' is 2 not 1. Your Python is coming 161 00:08:39,799 --> 00:08:43,799 to this position, and is checking this condition, 2 less than 162 00:08:43,799 --> 00:08:45,000 or equal to 5, yes, 'True'. 163 00:08:45,299 --> 00:08:48,400 Then again it will execute this logic. But while executing this 164 00:08:48,400 --> 00:08:52,799 logic now you're making your 'cnt' value from 2 to 2 + 1 = 3. 165 00:08:53,500 --> 00:08:56,799 Then with this 3 your Python will come to here and 3 less 166 00:08:56,799 --> 00:08:59,600 than equals to 5, yes, 'True'. Then it will execute once again. 167 00:08:59,600 --> 00:09:02,800 Now your 'cnt' becomes 4, 3 + 1 = 4. 168 00:09:04,000 --> 00:09:06,900 Then again, your Python will come to here with it, 'cnt' equals 169 00:09:06,900 --> 00:09:09,900 to 4, 4 less than or equal to 5, 'True'. Then it will execute 170 00:09:09,900 --> 00:09:14,300 once again this logic. Now 'cnt' value 5. With this 5 again 171 00:09:14,300 --> 00:09:16,800 your Python will come to this position, 5 less than or equal 172 00:09:16,800 --> 00:09:19,800 to 5, 'True'. 'True' means, again this logic will execute once. 173 00:09:21,300 --> 00:09:26,200 But now 5 + 1 = 6, and 6 less than or equals to 5? No, 'False'. 174 00:09:26,300 --> 00:09:30,300 If it is 'False', now your Python is going to stop your while loop. That's it. 175 00:09:30,300 --> 00:09:32,100 [no audio] 176 00:09:32,100 --> 00:09:37,200 Right. So here we are fixing how many times you want to repeat. 177 00:09:37,200 --> 00:09:39,100 [no audio] 178 00:09:39,100 --> 00:09:42,800 Right. Not only that, see based on other condition, 179 00:09:44,200 --> 00:09:48,900 while adding some number for your initial value, you are 180 00:09:48,900 --> 00:09:52,000 going to get your value more than this 181 00:09:52,000 --> 00:09:53,400 after certain number of times. 182 00:09:53,400 --> 00:09:55,100 You don't know how many times you want to repeat, 183 00:09:55,100 --> 00:09:59,400 but you know the condition. Once our value is more than this 184 00:09:59,400 --> 00:10:02,300 then I want to stop it. So here guys, be clear. 185 00:10:02,300 --> 00:10:05,000 We don't know how many times you are going, after how many 186 00:10:05,400 --> 00:10:07,300 times you are going to get this number we don't know. 187 00:10:08,400 --> 00:10:12,900 But you know that certainly, definitely after certain number of times 188 00:10:12,900 --> 00:10:17,000 your number will be more than this. At that situation 189 00:10:17,000 --> 00:10:18,000 you want to stop it, 190 00:10:19,200 --> 00:10:25,300 Right. So while loop in Python is used to repeat or iterate over 191 00:10:25,300 --> 00:10:28,900 a block of code, as long as the test expression or condition 192 00:10:28,900 --> 00:10:30,600 is true, right? 193 00:10:31,500 --> 00:10:32,600 So that's what we are doing here. 194 00:10:33,000 --> 00:10:34,900 As long as the condition result is true 195 00:10:34,900 --> 00:10:39,100 then it is going to execute this line. That is one case. Second 196 00:10:39,100 --> 00:10:45,600 one is, we use this loop when we don't know beforehand the 197 00:10:45,600 --> 00:10:47,100 number of times to iterate. 198 00:10:47,700 --> 00:10:50,800 That's what with this example. Really 199 00:10:50,800 --> 00:10:54,300 we don't know after how many times you're going to get this, 200 00:10:54,300 --> 00:10:58,500 I mean after how many iterations you are going to get your 201 00:10:58,500 --> 00:11:00,200 number more than this, we don't know. 202 00:11:00,200 --> 00:11:04,000 But we know that because of this calculation somewhere, 203 00:11:04,300 --> 00:11:08,200 somewhere you'll reach your number more than this, at that time 204 00:11:08,200 --> 00:11:09,200 I want to stop it. 205 00:11:09,600 --> 00:11:12,100 So that is logic of while loop. 206 00:11:13,400 --> 00:11:14,900 Right. So guys, be clear. 207 00:11:14,900 --> 00:11:18,300 One thing is to repeat your logic for infinite number of times. 208 00:11:18,300 --> 00:11:23,300 Second thing is to repeat your logic until the condition is true. 209 00:11:24,300 --> 00:11:28,300 And one more thing is, that is the important one, before starting 210 00:11:29,300 --> 00:11:33,500 your logic you don't know how many times you want to iterate your logic, 211 00:11:35,000 --> 00:11:37,900 but based on some condition you can stop it. 212 00:11:38,200 --> 00:11:42,000 So that condition you can use it in your while loop, okay. 213 00:11:43,000 --> 00:11:45,400 Okay guys, thank you for watching this video. While going 214 00:11:45,400 --> 00:11:50,000 forward we will see real-time use case of your while loop 215 00:11:50,100 --> 00:11:52,700 with many examples, okay. 216 00:11:52,700 --> 00:11:55,125 [no audio]