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