1 00:00:00,000 --> 00:00:03,450 Let's now discover what are loops and how to use 2 00:00:03,450 --> 00:00:06,360 them with Python. So let's first understand why 3 00:00:06,360 --> 00:00:09,000 you need loops. I have created a very simple 4 00:00:09,000 --> 00:00:11,910 program here that prints Hello 10 times. Okay, 5 00:00:12,120 --> 00:00:15,120 let's run that program. And actually, yes, it 6 00:00:15,120 --> 00:00:18,660 prints Hello 10 times. But that's quite not 7 00:00:18,660 --> 00:00:21,810 convenient. And now what if I asked you to print 8 00:00:21,810 --> 00:00:26,160 Hello 100 times, or 1000 times? Well, you're not 9 00:00:26,160 --> 00:00:29,910 going to write 1000 lines of print Hello, no. So 10 00:00:29,910 --> 00:00:32,430 whenever you need to repeat some instructions, 11 00:00:32,460 --> 00:00:35,040 let's say more than two or three times, this is 12 00:00:35,040 --> 00:00:37,980 where you are going to use loops. And we're going 13 00:00:37,980 --> 00:00:40,530 to start with the for loop. So I'm going to remove 14 00:00:40,560 --> 00:00:45,180 all that, and instead, I'm going to do for i in 15 00:00:45,270 --> 00:00:49,170 range 10. So let's just follow along, and then I'm 16 00:00:49,170 --> 00:00:54,390 going to explain to you, print hello. If I execute 17 00:00:54,390 --> 00:00:58,980 this, I still have Hello printed 10 times, okay. 18 00:00:59,760 --> 00:01:02,370 So what happened here. You can use the for loop, 19 00:01:02,370 --> 00:01:05,700 so you start with for, and then you're going to 20 00:01:05,730 --> 00:01:09,960 use an index, okay, so the index, which is going 21 00:01:09,990 --> 00:01:14,220 to go through a range or through a list, and the 22 00:01:14,220 --> 00:01:17,220 convention for that index is to name it, i okay. 23 00:01:17,220 --> 00:01:21,930 So for i, and then in, what you can do is use 24 00:01:21,960 --> 00:01:24,930 range. So basically, if you use range, a range is 25 00:01:24,930 --> 00:01:29,910 a list, which goes from zero to this value minus 26 00:01:29,910 --> 00:01:33,000 one. So basically, this is going to go between 27 00:01:33,000 --> 00:01:36,270 zero and nine. Okay, so you have 10 elements 28 00:01:36,270 --> 00:01:39,270 between zero and nine. And then you have a colon 29 00:01:39,270 --> 00:01:41,940 here. So what is going to happen is that this is 30 00:01:41,940 --> 00:01:45,000 going to execute the next block of code which is 31 00:01:45,090 --> 00:01:48,180 indented here, as you can see, is going to execute 32 00:01:48,180 --> 00:01:51,600 for each index, okay, that you have for the range, 33 00:01:51,630 --> 00:01:54,450 okay? So it's going to execute it for i is equal 34 00:01:54,450 --> 00:02:00,840 to 0,1,2,3,4,5,6,7,8, and 9. So basically, with that, 35 00:02:00,900 --> 00:02:04,290 you just execute that block of code 10 times, and 36 00:02:04,290 --> 00:02:07,380 the indentation here is super, super important. 37 00:02:07,620 --> 00:02:13,350 I'm going to print test here, okay. So Hello, and 38 00:02:13,350 --> 00:02:18,000 then test 10 times, I click, play to run. And you 39 00:02:18,000 --> 00:02:21,450 can see Hello, test, Hello, test, 10 times. Now I'm 40 00:02:21,450 --> 00:02:24,330 going to remove the indentation here and see what 41 00:02:24,330 --> 00:02:28,950 happens. We have Hello 10 times and then test is 42 00:02:29,010 --> 00:02:32,850 just one time, okay, because with for loop 43 00:02:32,910 --> 00:02:37,110 you're going to execute this for 10 times. Only the 44 00:02:37,740 --> 00:02:40,590 instructions that are indented in that block of 45 00:02:40,590 --> 00:02:43,980 code. And then you continue the execution back to 46 00:02:44,010 --> 00:02:46,050 the normal indentation and you continue the 47 00:02:46,050 --> 00:02:47,970 execution of your program. Okay, so the 48 00:02:48,210 --> 00:02:51,780 indentation is super important. And one thing I'm 49 00:02:51,780 --> 00:02:54,240 going to show you is we can so we can actually 50 00:02:54,240 --> 00:02:58,260 print that i, so that i will be here an integer. 51 00:02:58,560 --> 00:03:02,910 So I can do Hello, and then plus, let's see str 52 00:03:03,030 --> 00:03:08,820 i. Okay, I cast the number to a string. And 53 00:03:08,820 --> 00:03:12,330 let's run that. And you can see now I have Hello, 54 00:03:12,360 --> 00:03:17,160 0, Hello 1, etc. Until Hello 9. So you can 55 00:03:17,190 --> 00:03:20,160 actually see what's happening here. So the i is 56 00:03:20,160 --> 00:03:23,400 going to take all the values between zero and 10 57 00:03:23,430 --> 00:03:26,670 minus one and then we can use that inside the 58 00:03:26,670 --> 00:03:30,720 loop. Now if I want to print 100 time, I just put 59 00:03:30,720 --> 00:03:34,650 100 here, and I run, and you can see we print Hello 60 00:03:34,650 --> 00:03:37,140 with 100 time with a different number every 61 00:03:37,140 --> 00:03:39,840 time. Okay, so that's very powerful. Here we've 62 00:03:39,840 --> 00:03:44,000 just two lines of code, we can print this 100 times.