1 00:00:00,000 --> 00:00:01,500 Let's quickly talk about loops. 2 00:00:01,500 --> 00:00:05,900 Loops are a way to go through every item in a sequence. 3 00:00:05,900 --> 00:00:09,900 Now we know all about sequence types such as strings, lists, 4 00:00:09,900 --> 00:00:11,800 tuples, and sets. 5 00:00:11,890 --> 00:00:14,560 These, along with dictionaries, can actually be looped through. 6 00:00:14,560 --> 00:00:18,700 And we've actually seen examples of that throughout previous videos already. 7 00:00:18,700 --> 00:00:22,300 Now, when it comes to loops, in Python there are really two main loop types. 8 00:00:22,320 --> 00:00:26,790 You have a for loop and a while loop, and they are drastically 9 00:00:26,800 --> 00:00:28,860 different, but they do achieve the same thing. 10 00:00:29,080 --> 00:00:33,369 So a for loop is a loop that will loop through every item 11 00:00:33,380 --> 00:00:35,770 in a sequence until it's done, and then when it's done, it 12 00:00:35,780 --> 00:00:39,220 just automatically stops; whereas a while loop will run until 13 00:00:39,230 --> 00:00:43,160 you explicitly tell it to stop, and it will not automatically 14 00:00:43,170 --> 00:00:45,560 stop until it meets some sort of criteria. 15 00:00:45,560 --> 00:00:49,000 So as an example here, let's just create a simple loop. 16 00:00:49,000 --> 00:00:51,900 So let's loop through every letter in a word. 17 00:00:51,900 --> 00:00:55,300 So 'word = "Computer". 18 00:00:55,900 --> 00:01:02,200 Then we can do 'for letter in word: print(letter)', and this 19 00:01:02,200 --> 00:01:06,599 will just print out "C-o-m-p-u-t-e-r", computer, one letter at a time, 20 00:01:06,660 --> 00:01:07,910 because this is a sequence. 21 00:01:07,920 --> 00:01:11,520 This allows us to to loop through it one character at a time. 22 00:01:11,520 --> 00:01:13,100 This works with lists, too. 23 00:01:13,100 --> 00:01:15,950 So if we wanted to, we could cast this as a list. 24 00:01:15,960 --> 00:01:18,830 We haven't formally learned about casting yet, but this is 25 00:01:18,840 --> 00:01:21,230 going to basically turn this into a proper list. 26 00:01:21,240 --> 00:01:24,230 So let's go ahead and cast this as a list, and I'm going to 27 00:01:24,240 --> 00:01:25,820 insert a cell just below it. 28 00:01:25,940 --> 00:01:26,930 And let's do 'type( 29 00:01:27,200 --> 00:01:29,500 word)', is now a list. 30 00:01:29,500 --> 00:01:31,100 I'm going to insert a cell 31 00:01:31,100 --> 00:01:32,500 here. Let's see what 'word' is. 32 00:01:32,560 --> 00:01:34,360 It is a proper list. 33 00:01:34,700 --> 00:01:36,300 And let's rerun this. 34 00:01:36,300 --> 00:01:38,700 We're going to see the exact same results. 35 00:01:38,720 --> 00:01:40,250 Now, that is a basic for loop. 36 00:01:40,260 --> 00:01:42,560 All it did was loop through each 'letter', and when it was 37 00:01:42,570 --> 00:01:44,920 done it just stopped on its own. 38 00:01:44,930 --> 00:01:49,030 A while loop, on the other hand, will not. A while loop basically 39 00:01:49,040 --> 00:01:52,750 says, while something is true, do something, and it will go 40 00:01:52,750 --> 00:01:55,800 on forever until you tell it to stop. 41 00:01:55,800 --> 00:01:58,000 So be careful with this one, because this one can be a little bit 42 00:01:58,000 --> 00:01:59,100 dangerous on your computer. 43 00:01:59,100 --> 00:02:02,900 So what we're going to say here is, 'num = 1', 44 00:02:02,900 --> 00:02:08,000 and 'while num <= 100: 45 00:02:08,000 --> 00:02:10,600 [no audio] 46 00:02:10,600 --> 00:02:16,600 print(num)'. Now, because this number is set to 1, and it is not 47 00:02:16,600 --> 00:02:18,800 changing at all, ever, 48 00:02:18,860 --> 00:02:20,120 this will run forever. 49 00:02:20,130 --> 00:02:23,270 So what we need to do in here is build some sort of fail 50 00:02:23,270 --> 00:02:25,580 safe. We want this number to eventually reach the number 51 00:02:25,580 --> 00:02:29,000 100. So we say 'num = num + 1', 52 00:02:29,900 --> 00:02:32,800 And after every iteration, this will add 1. 53 00:02:32,800 --> 00:02:39,200 So this will go 1-2-3-4...97-98-99-100. And let's run this. 54 00:02:39,200 --> 00:02:45,100 And here we can see it's going to go all the way down to 100. 55 00:02:45,190 --> 00:02:47,800 In the next two lessons, we are going to learn more about 56 00:02:47,810 --> 00:02:50,740 for loops, and we're going to learn more about while loops, 57 00:02:50,740 --> 00:02:53,500 and looping through things like dictionaries as well. 58 00:02:53,560 --> 00:02:55,660 So this was just a quick little overview. 59 00:02:55,670 --> 00:02:58,270 You don't have to know exactly what I was talking about, about 60 00:02:58,280 --> 00:02:59,260 anything in this lesson. 61 00:02:59,260 --> 00:03:02,410 Really, this was just sort of a little bit of an introduction into 62 00:03:02,410 --> 00:03:04,100 for loops and while loops. 63 00:03:04,100 --> 00:03:06,500 We'll dive into those in their own videos. 64 00:03:06,500 --> 00:03:08,000 They deserve their own videos.