1 00:00:00,660 --> 00:00:04,290 The while loop is wonderful for condition based looping. 2 00:00:04,290 --> 00:00:06,970 While this condition is true, run this code. 3 00:00:06,970 --> 00:00:09,810 There's another type of looping that I'd like to show off here. 4 00:00:09,810 --> 00:00:13,450 And it's for when you want to iterate through a set of values. 5 00:00:13,450 --> 00:00:15,330 For instance, think back to our banner example, 6 00:00:15,330 --> 00:00:17,700 that we used to talk about strings. 7 00:00:17,700 --> 00:00:21,530 And we can actually loop over each letter in this banner. 8 00:00:21,530 --> 00:00:25,160 We can say for each letter in this banner, run some code. 9 00:00:25,160 --> 00:00:26,820 Hey, let's do that in the shell here. 10 00:00:27,930 --> 00:00:28,920 I'll set up the banner object. 11 00:00:30,550 --> 00:00:36,425 Banner equals Happy birthday. 12 00:00:36,425 --> 00:00:43,730 So, the keyword is for and we're gonna say letter in banner. 13 00:00:43,730 --> 00:00:45,150 Reads very nice, doesn't it? 14 00:00:45,150 --> 00:00:48,830 So for each letter in the banner, I'm gonna press enter and 15 00:00:48,830 --> 00:00:51,310 you'll notice that there's three dots now instead of the chevrons, 16 00:00:51,310 --> 00:00:54,310 instead of the three greater than signs three dots. 17 00:00:54,310 --> 00:00:57,390 And that's because it's waiting for us to finish this block of code. 18 00:00:57,390 --> 00:01:03,860 So I'm going to go one, two, three, four and we'll print letter.upper. 19 00:01:03,860 --> 00:01:09,010 So what's happening, is each time through, it's setting that letter variable. 20 00:01:10,220 --> 00:01:13,090 And now, I still have dots because it's waiting for me to finish. 21 00:01:13,090 --> 00:01:13,680 So, here we go. 22 00:01:14,830 --> 00:01:15,570 Happy birthday. 23 00:01:15,570 --> 00:01:16,890 All capitals. 24 00:01:16,890 --> 00:01:21,850 What happens here, is that each time through the loop, each one of these loops, 25 00:01:21,850 --> 00:01:27,070 this letter variable is set and you can use it in the body of the function. 26 00:01:28,240 --> 00:01:33,860 The loop runs through each item on this side of the in keyword and 27 00:01:33,860 --> 00:01:38,090 creates a new variable here on the left side, that value. 28 00:01:38,090 --> 00:01:43,010 The right side of this in key word has one requirement. 29 00:01:43,010 --> 00:01:45,280 It must be iterable. 30 00:01:45,280 --> 00:01:48,600 Now I find it helpful to break that word iterable down to 31 00:01:48,600 --> 00:01:50,940 let's be able to be iterated. 32 00:01:50,940 --> 00:01:55,450 Cuz what we are doing here is looping through or accessing each element. 33 00:01:55,450 --> 00:01:57,640 We're iterating through the values. 34 00:01:57,640 --> 00:02:00,100 Now strings have been designed to be iterable. 35 00:02:00,100 --> 00:02:04,220 Each step through the loop provides the next letter in the iteration. 36 00:02:04,220 --> 00:02:07,300 There are also many types in Python that are iterable. 37 00:02:07,300 --> 00:02:12,080 And Python allows you to create your own types that you can also iterate on. 38 00:02:12,080 --> 00:02:14,930 We'll get to more iterable types here in future courses. 39 00:02:14,930 --> 00:02:16,680 Check the teacher's notes for more. 40 00:02:16,680 --> 00:02:20,050 Now, do you see the difference between the while and for loops? 41 00:02:20,050 --> 00:02:23,850 For just runs from the start to the finish of an iterable. 42 00:02:23,850 --> 00:02:27,540 It runs through each and every value it's given from the iterable. 43 00:02:28,800 --> 00:02:31,670 With the addition of functions and loops to your tool set, 44 00:02:31,670 --> 00:02:34,720 you are now ready to write some pretty powerful applications. 45 00:02:34,720 --> 00:02:36,280 You might not believe me yet but 46 00:02:36,280 --> 00:02:40,680 the skills that you've picked up thus far, input and output, conditional branching, 47 00:02:40,680 --> 00:02:44,900 math, exception handling, looping and functions, are the foundations for 48 00:02:44,900 --> 00:02:48,280 almost all of the rest of the work that you'll do in programming. 49 00:02:48,280 --> 00:02:50,050 There's always gonna be more to learn. 50 00:02:50,050 --> 00:02:52,750 But you're now armed with quite an arsenal of solutions for 51 00:02:52,750 --> 00:02:55,220 just about any problem that you might encounter. 52 00:02:55,220 --> 00:02:56,080 I get it though. 53 00:02:56,080 --> 00:02:59,270 It's hard to see how to combine all the different skills that you just picked up. 54 00:03:00,390 --> 00:03:01,330 Let's do this. 55 00:03:01,330 --> 00:03:04,400 Let's wrap this course up with a little simulation that will produce 56 00:03:04,400 --> 00:03:08,440 some working software that utilizes most of your new found Python super powers.