1 00:00:00,300 --> 00:00:02,940 Instructor: Let's talk about loops. 2 00:00:02,940 --> 00:00:06,390 We've talked about conditional operators, 3 00:00:06,390 --> 00:00:08,310 logical operators, 4 00:00:08,310 --> 00:00:11,010 but loops gives us a whole new power 5 00:00:11,010 --> 00:00:13,260 when it comes to our machines. 6 00:00:13,260 --> 00:00:16,650 The concept of loops or looping in programming 7 00:00:16,650 --> 00:00:18,970 is really, really powerful. 8 00:00:18,970 --> 00:00:21,990 We saw that with logical operators 9 00:00:21,990 --> 00:00:23,310 and conditional logic. 10 00:00:23,310 --> 00:00:25,800 We're able to skip lines in our program. 11 00:00:25,800 --> 00:00:28,080 So that we don't always go one, two, three, 12 00:00:28,080 --> 00:00:29,340 four, five, six, so on 13 00:00:29,340 --> 00:00:30,780 and so forth. 14 00:00:30,780 --> 00:00:34,020 But loops do a really interesting thing. 15 00:00:34,020 --> 00:00:35,850 It allows us to run 16 00:00:35,850 --> 00:00:36,990 lines of code, 17 00:00:36,990 --> 00:00:39,010 over and over and over 18 00:00:39,900 --> 00:00:41,220 and that's really powerful 19 00:00:41,220 --> 00:00:44,670 because that means we can run things thousands of times, 20 00:00:44,670 --> 00:00:46,650 millions of times. 21 00:00:46,650 --> 00:00:49,950 And this is where machines excel. 22 00:00:49,950 --> 00:00:51,510 Machines excel 23 00:00:51,510 --> 00:00:53,850 at doing small tasks, over 24 00:00:53,850 --> 00:00:55,800 and over really, really fast. 25 00:00:55,800 --> 00:00:57,360 Way better than humans. 26 00:00:57,360 --> 00:01:00,390 So loops are one of the most powerful features 27 00:01:00,390 --> 00:01:02,130 of programming languages. 28 00:01:02,130 --> 00:01:03,333 So how do we create one? 29 00:01:04,200 --> 00:01:08,820 Well, it's as simple as using, what we call 'for', 30 00:01:08,820 --> 00:01:12,720 or in Python, we call them 'for' loops. 31 00:01:12,720 --> 00:01:14,610 With this keyword 'for', 32 00:01:14,610 --> 00:01:16,950 we're able to say 'for', 33 00:01:16,950 --> 00:01:18,690 let's say item, 34 00:01:18,690 --> 00:01:22,263 in zero to mastery, 35 00:01:23,250 --> 00:01:24,780 do something. 36 00:01:24,780 --> 00:01:26,970 So they're kind of like conditional operators 37 00:01:26,970 --> 00:01:29,940 instead of 'if' we have 'for', 38 00:01:29,940 --> 00:01:32,100 but then we have this 39 00:01:32,100 --> 00:01:35,490 thing, right, where we have item, which, 40 00:01:35,490 --> 00:01:37,560 what is that we, we don't really know. 41 00:01:37,560 --> 00:01:40,260 Well, this is a variable that we create. 42 00:01:40,260 --> 00:01:42,060 We can name it whatever we want. 43 00:01:42,060 --> 00:01:43,380 We can say I, 44 00:01:43,380 --> 00:01:45,030 We can say 45 00:01:45,030 --> 00:01:46,740 teddybears, 46 00:01:46,740 --> 00:01:47,640 whatever you want. 47 00:01:47,640 --> 00:01:49,320 This is a variable. 48 00:01:49,320 --> 00:01:52,740 And a variable is created here, 49 00:01:52,740 --> 00:01:55,200 for each item 50 00:01:55,200 --> 00:01:56,550 after the 'in'. 51 00:01:56,550 --> 00:02:01,550 So it's saying, hey for every item in zero to mastery, 52 00:02:01,590 --> 00:02:02,883 do something. 53 00:02:04,050 --> 00:02:06,453 And we've seen this 'in' keyword before, 54 00:02:07,494 --> 00:02:10,530 and we'll dig deep into this later, 55 00:02:10,530 --> 00:02:12,750 but we call this an iterable. 56 00:02:12,750 --> 00:02:16,740 An iterable is something that is able to get looped over. 57 00:02:16,740 --> 00:02:20,250 So an iterable allows us to use this notation 58 00:02:20,250 --> 00:02:24,450 of a 'for' something 'in' an iterable, 59 00:02:24,450 --> 00:02:27,360 to iterate over each item. 60 00:02:27,360 --> 00:02:30,690 In our case, if I print item here 61 00:02:30,690 --> 00:02:31,773 and I click run, 62 00:02:33,593 --> 00:02:34,710 look at that. 63 00:02:34,710 --> 00:02:37,860 It prints each item in the iterable, 64 00:02:37,860 --> 00:02:39,780 which is the string zero to mastery. 65 00:02:39,780 --> 00:02:42,990 So every letter, it goes into every 66 00:02:42,990 --> 00:02:45,000 bookshelf in our machine's memory 67 00:02:45,000 --> 00:02:48,270 and prints each item one at a time. 68 00:02:48,270 --> 00:02:50,210 Now, this works with strings, 69 00:02:50,210 --> 00:02:53,070 but we can also use lists, 70 00:02:53,070 --> 00:02:55,320 like one, two, three, four, five, 71 00:02:55,320 --> 00:02:56,700 that we've seen before. 72 00:02:56,700 --> 00:02:58,050 If I click run, 73 00:02:58,050 --> 00:03:00,360 again this is an iterable, 74 00:03:00,360 --> 00:03:02,160 we're able to iterate over it 75 00:03:02,160 --> 00:03:04,590 and it grabs each item 76 00:03:04,590 --> 00:03:05,820 in the list. 77 00:03:05,820 --> 00:03:07,080 Let's do a 78 00:03:07,080 --> 00:03:08,070 set. 79 00:03:08,070 --> 00:03:10,083 Could we do this with a set? 80 00:03:11,010 --> 00:03:12,303 Well, if I click run, 81 00:03:13,170 --> 00:03:14,340 that works as well. 82 00:03:14,340 --> 00:03:15,933 What about a topple? 83 00:03:17,100 --> 00:03:18,510 Let's do it with a topple. 84 00:03:18,510 --> 00:03:21,690 If I click run, that works as well. 85 00:03:21,690 --> 00:03:23,160 That's amazing. 86 00:03:23,160 --> 00:03:25,560 What about a dictionary? 87 00:03:25,560 --> 00:03:28,233 Well, we'll get to dictionaries in a bit. 88 00:03:29,250 --> 00:03:31,830 So for loops, allow us to iterate 89 00:03:31,830 --> 00:03:35,220 over anything that has a collection of items. 90 00:03:35,220 --> 00:03:36,690 So that in this case, 91 00:03:36,690 --> 00:03:39,390 we're looping one, two, three, four, five times. 92 00:03:39,390 --> 00:03:41,700 And you can see over here that we have the colon, 93 00:03:41,700 --> 00:03:45,030 and then the indentation to tell Python, 94 00:03:45,030 --> 00:03:48,180 hey, whatever comes here, 95 00:03:48,180 --> 00:03:50,310 I can print item again. 96 00:03:50,310 --> 00:03:52,410 I can print item again. 97 00:03:52,410 --> 00:03:54,330 I can do it as many times as possible, 98 00:03:54,330 --> 00:03:56,490 as long as I have indentation, 99 00:03:56,490 --> 00:03:58,410 it's going to keep printing 100 00:03:58,410 --> 00:03:59,730 our 101 00:03:59,730 --> 00:04:00,960 numbers, 102 00:04:00,960 --> 00:04:03,040 but as soon as I open up 103 00:04:04,050 --> 00:04:05,430 here 104 00:04:05,430 --> 00:04:07,833 and print something else, 105 00:04:09,360 --> 00:04:14,360 well, I only get that once because it's not in the loop. 106 00:04:14,640 --> 00:04:17,610 So our program first runs this, 107 00:04:17,610 --> 00:04:20,430 for one, so it's going to print one, three times, 108 00:04:20,430 --> 00:04:22,380 then two, then three, then four, then five, 109 00:04:22,380 --> 00:04:25,800 and then only finally then it goes to print. 110 00:04:25,800 --> 00:04:28,080 You see how we're looping over 111 00:04:28,080 --> 00:04:29,403 and over and over. 112 00:04:30,570 --> 00:04:32,250 Now what happens if I try 113 00:04:32,250 --> 00:04:33,783 and print item here? 114 00:04:35,610 --> 00:04:36,693 And I click run. 115 00:04:37,590 --> 00:04:38,910 Hmm. 116 00:04:38,910 --> 00:04:39,903 Did you see that? 117 00:04:41,160 --> 00:04:44,370 Item gets printed at the very end here? 118 00:04:44,370 --> 00:04:45,510 I don't know if you can notice it, 119 00:04:45,510 --> 00:04:47,310 but you see that there's four fives. 120 00:04:47,310 --> 00:04:50,280 We've printed one, two, three, four, 121 00:04:50,280 --> 00:04:52,890 each, three times, but five gets printed, 122 00:04:52,890 --> 00:04:56,520 four times because this last print 123 00:04:56,520 --> 00:04:59,070 at the very end is, 124 00:04:59,070 --> 00:05:00,480 well, five. 125 00:05:00,480 --> 00:05:02,823 Because by the time the loop ends, item, 126 00:05:03,660 --> 00:05:05,973 the value of item is actually five. 127 00:05:06,870 --> 00:05:07,740 Let me ask you this. 128 00:05:07,740 --> 00:05:10,590 Can I do something like for, 129 00:05:10,590 --> 00:05:14,520 let's say X, in another list 130 00:05:14,520 --> 00:05:17,370 that contains strings, a, b, 131 00:05:17,370 --> 00:05:18,363 and c. 132 00:05:19,260 --> 00:05:22,230 Could I do something like this? 133 00:05:22,230 --> 00:05:23,310 Absolutely! 134 00:05:23,310 --> 00:05:26,580 I can nest things in Python. 135 00:05:26,580 --> 00:05:27,990 And as a matter of fact, 136 00:05:27,990 --> 00:05:30,840 when we do conditionals in Python, 137 00:05:30,840 --> 00:05:32,880 like if statements, 138 00:05:32,880 --> 00:05:35,310 we can always nest those as well. 139 00:05:35,310 --> 00:05:39,000 Because in Python it's always the indentation, right? 140 00:05:39,000 --> 00:05:42,150 So I can say print here, item, 141 00:05:42,150 --> 00:05:43,740 and then also 142 00:05:43,740 --> 00:05:44,910 print, and you know what? 143 00:05:44,910 --> 00:05:46,568 Let's print them one next to the other. 144 00:05:46,568 --> 00:05:50,430 So I'm going to say print item and print X. 145 00:05:50,430 --> 00:05:51,513 If I click run, 146 00:05:54,390 --> 00:05:56,520 you see that we're printing, 147 00:05:56,520 --> 00:05:57,870 one and a, 148 00:05:57,870 --> 00:05:58,860 one and b, 149 00:05:58,860 --> 00:05:59,700 one and c, 150 00:05:59,700 --> 00:06:01,860 because item we 151 00:06:01,860 --> 00:06:03,090 run line one. 152 00:06:03,090 --> 00:06:05,493 So item is currently one. 153 00:06:06,630 --> 00:06:09,061 So we're going to go to the next line, line two, 154 00:06:09,061 --> 00:06:10,650 and we're going to say, 155 00:06:10,650 --> 00:06:13,200 we're going to loop over this iterable 156 00:06:13,200 --> 00:06:15,750 and we're going to say, this is X. 157 00:06:15,750 --> 00:06:19,800 So this is going to be one, this is going to be a. 158 00:06:19,800 --> 00:06:22,275 And then Python comes back to line two, 159 00:06:22,275 --> 00:06:24,450 because we're still in this loop, 160 00:06:24,450 --> 00:06:26,580 and we're going to say, hey, what's X now? 161 00:06:26,580 --> 00:06:28,710 Well, we're done with a, so let's go to b. 162 00:06:28,710 --> 00:06:30,630 So it's going to run b 163 00:06:30,630 --> 00:06:32,190 and then c. 164 00:06:32,190 --> 00:06:35,220 And only once this is done, 165 00:06:35,220 --> 00:06:36,900 does it go back out, 166 00:06:36,900 --> 00:06:38,790 and now starts with two, 167 00:06:38,790 --> 00:06:41,970 which is right here, two a, two b, two c. 168 00:06:41,970 --> 00:06:43,830 So you can have nested loops 169 00:06:43,830 --> 00:06:45,993 over and over and over as well. 170 00:06:47,040 --> 00:06:49,080 Right, so these are loops. 171 00:06:49,080 --> 00:06:50,403 And right now, 172 00:06:51,270 --> 00:06:55,500 maybe it's not completely obvious why these may be useful, 173 00:06:55,500 --> 00:06:57,870 but before we get into that I want to talk about, 174 00:06:57,870 --> 00:07:00,783 this idea of iterable in the next video.