#100DaysOfCode in Python Transcripts
Chapter: Days 19-21: Iteration with itertools
Lecture: Concepts: what did we learn?
0:00 Well, I hope you really enjoyed itertools, because it's really one of our favorite modules. It's just so much fun to use, and saves you so much time.
0:11 So, without further ado, what did we cover? The first thing we covered was itertools.cycle. Okay, so itertools.cycle,
0:22 we just imported cycle from itertools. Go figure. Then, we specified the iterable. Okay, the iterable that we wanted cycle to go over, okay?
0:34 and for our little exercise there, it was those weird dashes, so that we can make a little scroller.
0:40 If you were to use a string, such as the word string or my name, Julian, it would then cycle through those letters
0:47 in the same way that it's cycling through those. Okay? And that's why cycle is so easy to use. It does exactly what its name implies. It's awesome.
0:59 Then, what we did was we threw it in a while loop, while True, so that it was infinite, it would just keep running while this app was live.
1:08 And then we pumped that.. Next, we pumped that cycle out to standard out. That way it would work well on the command line. Okay?
1:19 Using Next, we were able to actually go through one iterable at a time. One iteration at a time. Okay, so we know each iteration, each cycle
1:32 is going to be one of these characters here. So, Next got us to pull just that one in the first loop.
1:40 Then, in the Next loop, it then took the backslash, and then the pipe, and then the forward slash. Okay? That's how this works here.
1:49 And then we just threw in a little time.sleep, just to slow things down a bit. Next we have itertools.product. So again, we imported product, okay?
1:58 And then what we did was we used repeat, okay? We used the repeat inside product to say how many combinations we wanted.
2:10 How many times we wanted a single iterable to be repeated or any iteration to be repeated. Okay? So by specifying two, when we iterate over my name,
2:24 we were able to start getting a giant list just like this. Okay? So J matched with J, J and then U, and so on and so forth. Okay?
2:34 And that's what product is, it's a Cartesian product, it gives you every possible combination. Okay?
2:41 It doesn't matter in this instance with the doubles, where you've got J and J. There's no sort of behind the scenes differing index there to say,
2:51 "Hey this is one J, and this is another one. "So there needs to be double." No. It's just a J, okay? Then we moved on to combinations and permutations,
3:02 one of my favorites. Imported, nice and easy. And then we created a quick list of super friends, Mike, Bob, and Julian.
3:11 And this was for us to then use as an example. So we got the combinations, okay? In sets of two... Of Mike, Bob, and Julian.
3:23 And you can see we have Mike Bob, Mike Julian, and Bob and Julian. And then that's when we pointed out that, well, there's no order here.
3:32 It takes it into account that, okay Mike and Bob both exist in this one tuple and therefore that's criteria met, that's a combination.
3:42 But, if we wanted the order to change, if we wanted to take that into account, that's where permutations comes into it. We have Mike and Bob here,
3:52 but then we also have Bob and Mike over here. So the order actually makes a difference. And that's why permutations is just as awesome as combinations.
4:03 And that was it. So your turn. If you haven't yet, I would strongly recommend, go and do the traffic lights, okay?
4:13 Try not to watch the video until you've actually done it. But at this point, you've probably already completed that. So that was for day two.
4:20 So we're looking at the third day of this lesson set. For this, go back to the three day overview,
4:27 if you've forgotten what it is that we were talking about, but essentially there are a few bites to go to the Code Challenges Platform,
4:34 and you have free access to those and all three of them have to do with itertools, and they're actually quite fun. So if you haven't done that,
4:43 go and give that a crack for day three. Enjoy. Keep calm and code.