1 00:00:00,000 --> 00:00:01,950 Okay, let's talk about for loops. 2 00:00:01,960 --> 00:00:04,500 We've actually seen these in several videos so far just 3 00:00:04,500 --> 00:00:05,500 as little examples. 4 00:00:05,500 --> 00:00:08,500 But a for loop is really just something that's going to 5 00:00:08,580 --> 00:00:11,870 loop through an iterable until it reaches the end, and then 6 00:00:11,870 --> 00:00:12,800 it stops on its own. 7 00:00:12,800 --> 00:00:16,000 Now, we haven't talked too much about iterables, but an iterable 8 00:00:16,000 --> 00:00:19,210 is anything that can be looped over. And up until this point, 9 00:00:19,220 --> 00:00:23,230 every sequence that we've worked with a string, list, tuple, 10 00:00:23,410 --> 00:00:30,110 set, even dictionary items, those are all loopable, so they're 11 00:00:30,110 --> 00:00:33,500 iterable. So let's just go through some examples of for 12 00:00:33,500 --> 00:00:37,200 loops. So let's create a list, and this list is going to be 'pets' 13 00:00:37,200 --> 00:00:44,400 ["Cat", "Dog", "Bird", "Goat"]', because goats are funny. 14 00:00:44,400 --> 00:00:48,900 So just to confirm what we have, we have ["Cat", "Dog", "Bird", "Goat"]' 15 00:00:49,600 --> 00:00:54,300 And so we can say, 'for', an animal, 'animal in pets', 16 00:00:54,300 --> 00:00:56,940 and we could do something strange here where we can replace 17 00:00:57,120 --> 00:01:01,920 all the a's with a letter 4 and all the o's with the letter 18 00:01:01,950 --> 00:01:04,590 0, or number 4, number 0. 19 00:01:04,920 --> 00:01:07,970 And this could be an exclamation mark, just to turn it upside 20 00:01:07,980 --> 00:01:09,440 down. So it's going to look a little weird. 21 00:01:09,450 --> 00:01:10,310 But we could do that. 22 00:01:10,310 --> 00:01:13,200 So we could say 'animal = animal', 23 00:01:13,280 --> 00:01:19,860 actually, let's back up a second. 'print(animal)', and it's going 24 00:01:19,870 --> 00:01:21,750 to print out each item one at a time. 25 00:01:22,700 --> 00:01:26,500 So now what we can do is, 'for animal in pets', 26 00:01:27,900 --> 00:01:31,700 we can now use this 'animal' because it's going to be 'animal = "Cat"', 27 00:01:31,700 --> 00:01:34,600 'animal = "Bird"', 'animal = "Dog"', 28 00:01:34,600 --> 00:01:36,100 'animal = "Goat"'. 29 00:01:36,140 --> 00:01:38,120 Now we can do 'animal = animal', 30 00:01:38,180 --> 00:01:42,060 and because we know that this is a string, we have a few 31 00:01:42,060 --> 00:01:45,900 different things that we can work with here. Such as the 32 00:01:45,900 --> 00:01:46,900 'replace' method. 33 00:01:46,900 --> 00:01:50,100 So we can replace "a" with a "4", 34 00:01:50,180 --> 00:01:55,140 we can '.replace("o", "0"), 35 00:01:55,500 --> 00:02:03,400 and we can replace any "i" with an "!", 36 00:02:03,400 --> 00:02:10,600 and then we can say, 'print("The animal with funky letters is", 37 00:02:11,400 --> 00:02:14,000 and we'll just use an f-string. 38 00:02:14,000 --> 00:02:16,900 Make sure we put an 'f' in front of that, because that's an f-string. 39 00:02:16,900 --> 00:02:17,900 And let's run this. 40 00:02:18,600 --> 00:02:21,200 "The animal with funky letters is C4t", "Cat". 41 00:02:21,250 --> 00:02:25,090 So the "a" turned into a "4", the "o" turned into a "0", the 42 00:02:25,100 --> 00:02:29,300 "!" turned into, the "i" turned into "!", 43 00:02:29,300 --> 00:02:33,400 and "Goat" got both "o" and "a", turned into a "0" and "4". 44 00:02:33,450 --> 00:02:36,570 And so basically, what this for loop is letting us do is 45 00:02:36,580 --> 00:02:40,050 loop through all the items in this list in order, and we 46 00:02:40,060 --> 00:02:41,550 can do anything we want. 47 00:02:41,550 --> 00:02:45,800 We can change and work with that particular variable. 48 00:02:45,860 --> 00:02:48,770 And in every single iteration all we're doing is, saying 49 00:02:48,770 --> 00:02:50,400 'animal = "Cat"', 50 00:02:50,420 --> 00:02:53,420 the 'animal = "Dog"', the 'animal = "Bird"', the 51 00:02:53,420 --> 00:02:54,500 'animal = "Goat"'. 52 00:02:55,400 --> 00:02:57,500 So now let's go through this example with a set. 53 00:02:58,600 --> 00:02:59,600 So we have 'pets'. 54 00:03:00,700 --> 00:03:01,800 That's a list. 55 00:03:01,870 --> 00:03:05,140 But what we can do is turn 'pets' into a set by casting it, 56 00:03:05,320 --> 00:03:09,580 and this is the exact same thing as saying 'pets = 57 00:03:09,590 --> 00:03:14,210 ' 58 00:03:14,210 --> 00:03:16,900 And actually, to keep things simple, let's not cast that as a set. 59 00:03:17,000 --> 00:03:20,800 Let's just use this, and 'print(pets)'. 60 00:03:21,700 --> 00:03:25,600 And so now it looks almost the exact same, but it's in a 61 00:03:25,600 --> 00:03:28,700 set. So we can't have multiple cats, multiple goats, anything like that. 62 00:03:28,740 --> 00:03:31,870 So let's add a second "Goat", and it still just shows up with 63 00:03:31,880 --> 00:03:32,800 one individual "Goat". 64 00:03:32,900 --> 00:03:38,900 Now, sets do not keep their original input or their input order, rather. 65 00:03:38,900 --> 00:03:41,750 So just because it says, '' doesn't mean 66 00:03:41,750 --> 00:03:45,900 it's always going to be ', it's already in the wrong order, "Goat". 67 00:03:46,900 --> 00:03:50,700 But every item in this particular sequence is going to be completely unique. 68 00:03:50,700 --> 00:03:55,000 So now we could say, 'for set_animal', we just use a different 69 00:03:55,000 --> 00:03:59,800 variable name here, 'in pets: print(set_animal)'. 70 00:03:59,800 --> 00:04:03,200 [no audio] 71 00:04:03,200 --> 00:04:05,500 And let's add a second parameter to our 'print' statement 72 00:04:05,500 --> 00:04:07,800 just to switch it up, "is the animal". 73 00:04:09,500 --> 00:04:13,200 And there we go, "Cat", "Dog", "Goat", and "Bird". 74 00:04:13,900 --> 00:04:18,600 And so the syntax that we're looking at here is 'for', 75 00:04:18,649 --> 00:04:21,950 and then whatever your variable name is going to be, 'in', whatever 76 00:04:21,950 --> 00:04:25,399 sequence you're looping through, whatever iterable you're looping through. 77 00:04:25,399 --> 00:04:28,600 Now, it's called an iterable, because we can iterate through each one. 78 00:04:28,600 --> 00:04:32,400 We can iterate through 'Cat', 'Dog', 'Bird', and 'Goat' 79 00:04:32,400 --> 00:04:34,600 Tuples work the exact same way, 80 00:04:34,600 --> 00:04:36,800 so I'm not even going to show you an example of that because 81 00:04:36,800 --> 00:04:40,800 it is literally the exact same thing as a list. 82 00:04:40,800 --> 00:04:43,620 We can also loop through letters in a word. 83 00:04:43,620 --> 00:04:49,600 So let's say the 'word' is going to be "Pythonisthebest". Because 84 00:04:49,600 --> 00:04:51,060 I said word instead of sentence, 85 00:04:51,070 --> 00:04:52,290 so I'm going to stick with it. 86 00:04:52,500 --> 00:04:56,800 'for letter in word: print(letter)'. 87 00:04:56,810 --> 00:04:59,300 And this is just going to go through each letter in here, 88 00:04:59,300 --> 00:05:03,640 assign every letter to the variable called 'letter', and print 89 00:05:03,640 --> 00:05:06,000 it out one by one, just like that. 90 00:05:06,000 --> 00:05:08,200 [no audio] 91 00:05:08,200 --> 00:05:10,100 Now I'm going to show you an example that's going to get 92 00:05:10,200 --> 00:05:13,610 a little more advanced, and by no means you need to be familiar 93 00:05:13,610 --> 00:05:14,600 with this right now, 94 00:05:14,600 --> 00:05:17,790 but I would like you to get some eyes on something a little 95 00:05:17,800 --> 00:05:19,260 more complicated here. 96 00:05:19,270 --> 00:05:25,020 So let's loop through a tuple of tuples, and this tuple 97 00:05:25,020 --> 00:05:27,500 of tuples is going to look very, very strange at first. 98 00:05:27,500 --> 00:05:30,700 So let's say we have a tuple of 'people'. 99 00:05:30,740 --> 00:05:34,400 And in this particular tuple, let's even put this on a new 100 00:05:34,400 --> 00:05:38,600 line, we have the age of someone, 30; and the name. 101 00:05:39,300 --> 00:05:40,700 Okay, that's one tuple. 102 00:05:41,100 --> 00:05:42,420 Let's put another one in here. 103 00:05:42,500 --> 00:05:44,300 We've got 5, 104 00:05:45,800 --> 00:05:47,900 and the name, "Zephyr". 105 00:05:47,900 --> 00:05:49,300 Let's put another one in here. 106 00:05:49,900 --> 00:05:51,800 "Henry" is going to be 3 this year, 107 00:05:52,800 --> 00:05:53,800 and "Henry". 108 00:05:54,940 --> 00:05:58,510 And when we run 'people', we can see that it gets flattened. 109 00:05:58,700 --> 00:06:00,700 And this is a tuple of tuples. 110 00:06:01,040 --> 00:06:02,660 So the whole thing is a tuple. 111 00:06:02,660 --> 00:06:03,650 So it's an iterable. 112 00:06:03,920 --> 00:06:07,400 And inside of it is a pair of tuples. 113 00:06:07,410 --> 00:06:11,180 So we've got one item that's an age, and the second item 114 00:06:11,180 --> 00:06:12,200 is always a name. 115 00:06:12,200 --> 00:06:14,270 So we're going to loop through these now. 116 00:06:14,280 --> 00:06:19,350 So we're going to say, 'for age, name in people', 117 00:06:19,360 --> 00:06:21,690 and what this it's going to do, is loop through it, 118 00:06:21,700 --> 00:06:24,100 and it's going to say that first item is going to be age, 119 00:06:24,100 --> 00:06:26,000 and that second item is going to be the variable name. 120 00:06:26,100 --> 00:06:34,900 So now we can say 'print()', with an 'f' statement, " is years old". 121 00:06:36,600 --> 00:06:40,700 And look at that. "Kalob is 30 years old", and "Zephyr is 5", and "Henry is 3 years 122 00:06:40,700 --> 00:06:43,000 old". Now, this is called unpacking. 123 00:06:43,030 --> 00:06:46,930 And what happens if one of these does not work out? 124 00:06:46,940 --> 00:06:50,770 So let's say this last one, Henry doesn't have a name. 125 00:06:50,800 --> 00:06:51,800 Let's rerun these. 126 00:06:52,800 --> 00:06:54,700 It will work until it doesn't, 127 00:06:54,700 --> 00:06:57,240 and we get this thing called a 'ValueError', and it says, "not 128 00:06:57,240 --> 00:06:58,600 enough values to unpack". 129 00:06:58,600 --> 00:07:00,500 So let's undo that, 130 00:07:00,500 --> 00:07:04,400 and let's see what happens when there are too many values. 131 00:07:04,400 --> 00:07:07,200 Let's just say, 'This is a third value'. 132 00:07:07,280 --> 00:07:08,900 Rerun these cells. 133 00:07:09,300 --> 00:07:13,200 And again, we can see that the first two work, but Henry 134 00:07:13,200 --> 00:07:15,920 has a third value, and this one is also a 'ValueError', 135 00:07:15,930 --> 00:07:18,230 but this one says, "too many values to unpack". 136 00:07:18,240 --> 00:07:22,370 So your tuple of tuples needs to have the exact same formatting 137 00:07:22,370 --> 00:07:23,600 through the entire thing. 138 00:07:24,900 --> 00:07:28,600 And when you do that, you can then unpack it. 139 00:07:28,610 --> 00:07:31,340 So this is variable age, and that's the name; 140 00:07:31,340 --> 00:07:34,600 variable age, variable name; variable age, variable name. 141 00:07:34,620 --> 00:07:37,710 Now, I'm not expecting you to know that right now, but this 142 00:07:37,720 --> 00:07:41,040 is a slightly more advanced feature in loops, and it is going 143 00:07:41,040 --> 00:07:44,100 to be very, very useful in the very near future. 144 00:07:44,100 --> 00:07:46,600 I believe in the next 2 or 3 videos, we are going to get 145 00:07:46,600 --> 00:07:49,700 into a little bit more unpacking when it comes to looping 146 00:07:49,700 --> 00:07:50,800 through a dictionary. 147 00:07:50,800 --> 00:07:54,800 Now, what I would like you to do, is create a list and then 148 00:07:54,820 --> 00:07:55,860 write a for loop. 149 00:07:55,870 --> 00:07:57,780 It does not need to be amazing. 150 00:07:57,780 --> 00:08:00,000 It does not need to be super complex. 151 00:08:00,000 --> 00:08:03,800 All it needs to do, is loop through every item in a list. 152 00:08:03,800 --> 00:08:06,300 Once you've learned how to loop through a list, it is the 153 00:08:06,300 --> 00:08:08,200 exact same for a tuple, 154 00:08:08,220 --> 00:08:10,890 exact same for set, the exact same for a string. 155 00:08:10,890 --> 00:08:11,966 [no audio]