1 00:00:00,300 --> 00:00:09,120 Let's talk about Loop's, we've talked about conditional operators, logical operators, but Loop's 2 00:00:09,120 --> 00:00:12,330 gives us a whole new power when it comes to our machines. 3 00:00:13,270 --> 00:00:18,400 The concept of loop's or looping in programming is really, really powerful. 4 00:00:19,300 --> 00:00:26,260 We saw that with logical operators and conditional logic were able to skip lines in our program so that 5 00:00:26,290 --> 00:00:30,010 we don't always go one, two, three, four, five, six, so on and so forth. 6 00:00:30,760 --> 00:00:33,690 But loops do a really interesting thing. 7 00:00:34,000 --> 00:00:39,010 It allows us to run lines of code over and over and over. 8 00:00:39,860 --> 00:00:45,890 And that's really powerful because that means we can run things thousands of times, millions of times, 9 00:00:46,610 --> 00:00:49,850 and this is where machines excel. 10 00:00:49,940 --> 00:00:57,110 Machines excel at doing small tasks over and over really, really fast, way better than humans. 11 00:00:57,350 --> 00:01:01,920 So loops are one of the most powerful features of programming languages. 12 00:01:02,120 --> 00:01:03,200 So how do we create one? 13 00:01:04,180 --> 00:01:13,480 Well, it's as simple as using what we call for or in Python, we call them for loops with this key 14 00:01:13,480 --> 00:01:24,400 word for we're able to say for let's say item in zero to mastery, do something. 15 00:01:24,790 --> 00:01:29,650 So they're kind of like conditional operators instead of if we have four. 16 00:01:29,890 --> 00:01:36,130 But then we have this thing right where we have item which what is that? 17 00:01:36,130 --> 00:01:37,330 We don't really know. 18 00:01:37,570 --> 00:01:40,210 Well, this is a variable that we create. 19 00:01:40,240 --> 00:01:42,030 We can name it whatever we want. 20 00:01:42,040 --> 00:01:43,120 We can say I. 21 00:01:43,360 --> 00:01:45,760 We can say teddybears. 22 00:01:46,750 --> 00:01:56,230 Whatever you want, this is a variable and a variable is created here for each item after the end. 23 00:01:56,530 --> 00:02:02,800 So saying, hey, for every item in 02 mastery, do something. 24 00:02:04,050 --> 00:02:06,270 And we've seen this in keyword before. 25 00:02:07,690 --> 00:02:14,770 And we'll dig deep into this later, but we call this an iterable, and iterable is something that is 26 00:02:14,770 --> 00:02:16,460 able to get looped over. 27 00:02:16,750 --> 00:02:25,990 So an iterable allows us to use this notation of a for something in an iterable to iterate over each 28 00:02:25,990 --> 00:02:26,440 item. 29 00:02:27,340 --> 00:02:31,600 In our case, if I print item here and I click run. 30 00:02:33,910 --> 00:02:39,780 Look at that, it prints each item in the iterable, which is the string zero to mastery. 31 00:02:39,790 --> 00:02:47,590 So every letter it goes into every bookshelf in our machine's memory imprints each item one at a time. 32 00:02:48,250 --> 00:02:55,510 Now, this works with strings, but we can also use lists like one, two, three, four or five that 33 00:02:55,510 --> 00:02:56,310 we've seen before. 34 00:02:56,710 --> 00:03:00,340 If I click run again, this is an iterable. 35 00:03:00,370 --> 00:03:05,320 We're able to iterate over it and it grabs each item in the list. 36 00:03:05,830 --> 00:03:07,720 Let's do a set. 37 00:03:08,080 --> 00:03:09,910 Can we do this with a set? 38 00:03:10,980 --> 00:03:12,120 Well, if I click run. 39 00:03:13,150 --> 00:03:14,110 That works as well. 40 00:03:14,350 --> 00:03:15,790 What about a couple? 41 00:03:17,090 --> 00:03:18,050 Let's do it at the top. 42 00:03:18,120 --> 00:03:19,340 In fact, like run. 43 00:03:20,510 --> 00:03:21,500 That works as well. 44 00:03:21,710 --> 00:03:22,430 That's amazing. 45 00:03:23,150 --> 00:03:25,310 What about a dictionary? 46 00:03:25,580 --> 00:03:28,130 Well, we'll get to dictionaries in a bit. 47 00:03:29,230 --> 00:03:36,700 So for loops, allow us to iterate over anything that has a collection of items so that in this case 48 00:03:36,700 --> 00:03:39,280 we're looping one, two, three, four, five times. 49 00:03:39,460 --> 00:03:46,060 And you can see over here that we have the Colline and then the indentation to tell Python, hey, whatever 50 00:03:46,060 --> 00:03:51,880 comes here, I can print item again, I can print item again. 51 00:03:52,390 --> 00:03:54,310 I can do it as many times as possible. 52 00:03:54,310 --> 00:04:02,800 As long as I have indentation, it's going to keep printing our numbers, but as soon as I open up. 53 00:04:04,040 --> 00:04:07,670 Here I print something else. 54 00:04:11,090 --> 00:04:18,290 I only get that once because it's not in the loop, so our program first runs this for one. 55 00:04:18,320 --> 00:04:23,180 So it's going to print one three times, then two, then three, then four and five, and then only 56 00:04:23,180 --> 00:04:25,360 finally then it goes to print. 57 00:04:25,790 --> 00:04:29,240 You see how we're looping over and over and over. 58 00:04:30,560 --> 00:04:33,650 Now, what happens if I try and print item here? 59 00:04:35,600 --> 00:04:36,530 And I click run. 60 00:04:38,970 --> 00:04:39,840 Did you see that? 61 00:04:41,170 --> 00:04:46,420 Item gets printed at the very end here, I don't know if you can notice it, but you see that there's 62 00:04:46,420 --> 00:04:47,230 four fives. 63 00:04:47,290 --> 00:04:51,280 We've printed one, two, three for each, three times. 64 00:04:51,280 --> 00:05:00,880 But five gets printed four times because this last print at the very end is, well, five, because 65 00:05:00,880 --> 00:05:02,680 by the time the loop ends item. 66 00:05:03,660 --> 00:05:05,880 The value of item is actually five. 67 00:05:06,890 --> 00:05:15,740 Let me ask you this, can I do something like for, let's say, X in another list that contains strings 68 00:05:15,740 --> 00:05:18,230 A, B and C? 69 00:05:19,270 --> 00:05:21,340 Could I do something like this? 70 00:05:22,190 --> 00:05:30,470 Absolutely, I can mess things in Python and as a matter of fact, when we do conditionals in Python 71 00:05:30,830 --> 00:05:37,970 like if statements, we can always nesse those as well, because in Python, it's always the indentation. 72 00:05:38,180 --> 00:05:38,540 Right. 73 00:05:38,990 --> 00:05:41,180 So I can say print here item. 74 00:05:42,140 --> 00:05:46,880 And then also print and you know what, let's print them one next to the other. 75 00:05:47,150 --> 00:05:51,350 So I'm going to say print item and print X if I click, run. 76 00:05:54,410 --> 00:06:02,810 You see that we're printing one and a one and B one and C because item we run line one. 77 00:06:03,080 --> 00:06:05,360 So item is currently one. 78 00:06:06,640 --> 00:06:12,160 So we're going to go to the next line, line two, and we're going to say we're going to loop over this 79 00:06:12,160 --> 00:06:20,560 iterable and we're going to say this is X. So this is going to be one this is going to be a and then 80 00:06:20,890 --> 00:06:26,020 Python comes back to line two because we're still in this loop and we're going to say, hey, what's 81 00:06:26,020 --> 00:06:26,580 X now? 82 00:06:26,590 --> 00:06:27,580 Well, we're done with. 83 00:06:27,700 --> 00:06:36,670 So let's go to be so it's going to run B and then C, and only once this is done does it go back out 84 00:06:36,850 --> 00:06:44,500 and now starts with two, which is right here to A, to B to C, so you can have nested loops over and 85 00:06:44,500 --> 00:06:45,850 over and over as well. 86 00:06:47,000 --> 00:06:55,070 All right, so these are loops and right now, maybe it's not completely obvious why this may be useful, 87 00:06:55,490 --> 00:07:00,650 but before we get into that, I want to talk about this idea of iterable in the next video.