 In this section we're going to look at Comprehensions. Now Comprehension is not from the word Comprehend to understand something. It actually just means generating stuff and collecting them somehow. So look at what we've done here. You see these square brackets. In a future lecture we're going to look at what these brackets really mean. But I'm running this very compact form of the for loop. It says N for N in 1 to 5. So if I just had the for N in 1 to 5 it was going to loop through. There's going to be an N statement but it's going to loop through N as 1, N as 2, N as 3, N as 4, N as 5. But I've got this notation N for N in inside of this. And if I execute this I'm going to have this variable called A. And it's going to be an array. An array is just a list of values really. And that is why this is called list comprehension. Comprehension I've generated 5 numbers. And I've placed it inside of this N value which I've attached to this computer variable called A. Let's make a little bit more sense of it. I'm not going to call it A now. I'm not going to store it anyway. I'm just going to have this list comprehension. So it's inside of these square brackets once again. And I'm going to say I cubed for I in negative 3 to 3. So it's going to take negative 3 is going to be my first. Negative 3 cubed is negative 27. Then negative 8, negative 1, etc. Let's see that that is executed. And again it's a 7 element array as it goes from negative 3 to 3. There's 7 values that cubes everyone and I've generated. I've generated and I've collected some items. So that is called comprehension. Just to show you if I say what is the type of this thing that I've just gotten back. And it is indeed an array. An array, look at it in a future lecture. This is this list of values. And there was my list. Now just to show you what the curly braces does. I'm doing the same thing. I cubed for I in and I'm just making it a bit shorter from negative 2 to 2. Let's see what the type of that is. Because everything inside there with the square brackets was an integer. What these curly braces are doing is just making all of the values from negative 27 to negative 28. It is attaching them to the type any. So that's the difference. Now I can combine things. I can run a row with inner row. So I've just called these rows and columns. So two things. Remember I said N for N in. Now I'm saying row comma column for row in comma column in. Let's see what's going to happen here. So it goes down the columns. Let me warn you like of that first. So it's going to say for row in 1 to 3. So there's my 1. Then it jumps to the second column for which it will be 1. Now row 2 but we're still with column 1. Row 3 we're still with column 1. Then it goes to the 2 for the row. So back to the row 1 I should say and then only does it go over to column 2. So we're going to have 1, 2, 2, 2, 3, 2. And then in a row first we're going to have 1, 3, 2, 3, 3, 3. So you can clearly see the order of execution of placing more than one variable in this list comprehension. Let's have a deeper look at that so you can see what comes after the other. Here I'm using the random keyword. It's going to generate, take a random value and I've asked it to choose from value 0 to 9. And I want to place it in a 3 by 3 grid. 3 rows is the first 3 columns. It's the second and I'm attaching them to this computer variable called m. There we go. Every time you run this it's going to give you different values because it'll be chosen at random. So I have 890176280. Now just to show you in what order this was done that it is down the columns. Remember things have an index. That is the number where it's placed. An index is a place where you run a race. Someone comes first, someone comes second, someone comes third. That is our index where you are placed. And I can use this enumerate keyword. So I'm going to have this list comprehension i for i in enumerate m. Now what enumerate does is the following. I'm just going to run that line of code so you can see what it does. So it goes through the values. Enumerate is going to return for me the index and the actual value. So see the 8 there. There's the 8. It was placed in position number 1. The 9 is below and that came in position number 2. So i for i. So it's a list comprehension. I'm going to generate this bunch of things and I'm going to gather them together in an array. So that's the list comprehension but the enumerate gives me the index and then the value. Just to show you that we're going down 890176 being values number 1, 2, 3, 4, 5, 6, 7, 8, 9. Let's hit it home. Let's have a look at this. Now I'm going to delete that because we needn't have that there. I'm going to have this computer variable called a random value selected from 1 to 5. Either 1, 2, 3, 4, 5 and I want to make a list of 20 of those. So I'm creating this computer variable called a and I'm putting in a 20 random values, each being between 1 and 5, 1 and 5 inclusive. I'm going to print to the screen this a. Now the way that a is generated is again as an array. Don't worry about an array. We're going to have a look at this and it's just going to print this whole list of 20 numbers for me. Now I'm going to have this create this computer variable called look for first. I can call it whatever I like. Look for first and I'm attaching to that the integer value 4. Because I'm going to run this very special for loop with an if statement inside of it. But because enumerate here, remember first I had a range operator I said for i in 1 to 5. Enumerate returns two things for me an index and then the value. I better have two computer variables because it does return enumerate returns two values. So the first value is the index. I've called that computer variable ix and the first one is the second one is the actual value the occurrence. I'm going to just call that CURR. I could call it whatever. Now I'm going to have this if statement if current CURR at least equals equals look for first and look for first is 4. So it's going to iterate through all these values inside of A which is generated there. It's going to say is this 4? Is it a 4? If not, you know that if statement is not going to be executed because of this break statement. So I'm showing you a lot of things inside of this little code cell. We'll run through it. Let's run it. Perhaps it would make better sense. So there's my A and I'm printing it to the screen and there is 20 values between 1 and 5, 1 and 5 including. Now what I want to do is I want to run through it one by one and I do it through the enumerate because the enumerate gives me two things back. An index and I create a variable for that and the actual value I create a value for that. I'm looking at the actual value and I'm asking is it 4? If it is, it's going to print this line. Remember if it's false, it just ends. The index of the first occurrence of the searched value that's the string is going to print out for me if this is 2. And this is we look at strings in the future, but if I put this little dollar sign in front of it, it's actually not going to print that text, but it's actually going to print the value inside of that variable. There's a very nice way of putting values inside of strings is Ix, then break. So look at this, 4 was an index number 2 and that's exactly what it prints out. The index of the first occurrence of the searched value 4 is index number 2. That is what I printed out there using those values. But then I used this break because once it's found that I don't want it to go on in my if statement, I want this whole thing just to stop executing because I only want that first occurrence of 4. Good. Let's carry on. There's another way to do this iteration and that is to do remember we had before when we created this. Let's go up here. When it went through rows first and then to the columns, rows again to the second column, I can do it all in one go with using this zip keyword for I in zip 1 to 10, 101 to 110, 501 to 510 print I. So it's what it does that it doesn't go through one at a time. It goes through all of them at once. So we'll have 1, 101, 501, 2, 102, 502 and prints them as such. So that's what zip does. Now what happens if they're not of equal length? So I've made the first one a little bit shorter. There's only 1 to 4. There's 5 elements there and there's 5 elements there. What Julia will do is we'll just take the shortest one, even if the shortest one is in the second set or even if it's in the last set, it's just going to take the shortest ones. So a lot for you to look at. You can re-watch this section of list comprehension. It's a bit difficult to get used to but quite powerful.