 Welcome back everyone. Today we're going to talk about arrays in Python. So just like before I have a just a text editor open, any notepad will do, and I have my command prompt open with Python installed. Okay, so if you look back at some of the previous lectures we've already talked about assigning variables. So we might have a variable for example x and we assign a value to that variable called hello. So this hello in quotes would be a string. We're assigning the contents of hello into the variable named x. So if we did something like print x, let's run our code. So I'm running Python and then our file that we have saved here is 004 arrays.py. If I run that, then it prints hello. Now it's not printing x, it's printing the contents that are saved inside x. Okay, so we've already talked about variables before in a prior lecture. And assigning variables is kind of a main concept that you need to get whenever you start programming in pretty much any programming language. But there's another structure that's also extremely valuable and that is something called an array. So an array, to create an array, it's almost like assigning a value to a variable except it's many values assigned to one variable. So you can think of it kind of like a list that we save inside a variable. So we could do something like, let's say we want to make a collection, call it food. And here the syntax is the same. So I have my variable name, just like x equals, I have food equals, and that means food is my variable name. And then equals means I'm assigning it some value. So let's say I want to add some food in a list. So let's say I'm adding taco, cookies, and kimchi. Okay, now notice what I've done here. I have the square brackets. And I start with a square bracket, and then I have one string, and then a comma, and quotation marks, and then cookies, or another string comma, and then another string, and I end with another square bracket. So these square brackets, anytime you see a square bracket like this, even if it's empty, you just see two square brackets, that means this variable is an array. So it's still a variable, but instead of holding a single string, it can hold an array or kind of like a list of things. Okay, so we have an array, and then I have three strings inside this array. So just to show you what's going on here, we can do print food. Now I'm printing food, which is the variable, and food currently contains three strings. Okay, so if we run that code again, we get our array, the entire list just like we had before. Now you might be thinking, okay, well, what's the difference between that and just a normal string? Well, we can treat each of these values like a separate entity. So for example, I can look up if kimchi is inside this list, I can check whether some value is inside the list, I can iterate through all of the values and do something with them. There's a lot of different things that we can do with arrays, which we'll talk about in a second. So I can go into this and check whether something is inside my variable. So for example, if I want to access food, and let's say that I want to know what was the second value inside our list, I could use the brackets again, use the brackets again and do instead of just printing food only, which gave us the entire list, I can do food and then square bracket, and then the index of the value that I want to reference. So in this case, food one would be cookies. Now you might be thinking, well, why isn't food one tacos? That's because this is, it starts with zero. So the value or the index for tacos would be zero, the index for cookies would be one, and the index for kimchi would be two. So arrays start with zero. So if I'm accessing the value number one, then I would be accessing cookies. So if we print this, let me comment this out, if we print this, then I should just get the value cookies. And that's what I get. So here we can see cookies prints. If I print zero, or if I want to access the value zero, it should print tacos. So that's how we can access particular values inside of an array. So if we're looking for a particular instance, we can just call it directly with its index if we know what index we're looking for. Now arrays, however, can change in size. So we can add things to them, we can remove things from them. So they could change length over time. So instead of accessing a single instance, or a single value inside the array, maybe I want to find out how long the array is. And for that, we can use something called le in or length. Okay, so we can print the length of food. And if we print that, we get three, because there are currently three values inside the array. Now notice, we're counting the values here. So we're not using the indexes. So this would be one value, two values, three values. Okay, so we can use the length of the array to find out how many things are inside of our array. And that comes in really handy in more advanced things we'll talk about later. Now, when would you actually use this? One way you use this is to iterate over all of the different values that are inside the array. So for example, we've already talked about loops. And one type of loop we talked about was a for loop. So let's go ahead and use a for loop. So we can say for, I'll say f, f is our value here, in food. And what I'm doing here is saying, for f, which is a new variable inside food, do something, and these two dots mean do something. So what this does is takes the array, and then whatever value is inside the array, it will take the first value, assign it to f, and then we can do something with it. And then the next time it loops around, it'll take the second value, assign it to f, and then we can do something, and then so on until the end of the array. Okay, so what that does for us is let's say we can print x, sorry, print f, which is our food value. And then guess what should happen, right? So we have the only thing we're printing now is f. So we're printing f in food. So we should basically get three separate lines, printing out each of these strings. Okay, so let's try that. Okay, so we get three separate lines in order, printing out the strings. Okay, and that's because we iterated through, we took the first value assigned it to f, and then we printed f, we took the second value assigned it to f, and then printed f. Okay, now compare that to if we're just printing food. Okay, so the first one prints everything in the array, and we don't actually treat this like separate objects, we're just treating it like one line. But whenever we have it in a loop, we're taking each each individual value and then doing something with it. Yeah, so you could do something like adding information like I like to eat. So this is an additional string. And then, as you saw probably in the first video, this plus f, this first part is a string, a long string. And then we have the plus sign, which is concatenation. And then we have another string in this variable. So what we should get is I like to eat tacos, I like to eat cookies, I like to eat kimchi. Okay, so let's try that. Okay, so we have both of our strings concatenated together, and we've accessed each individual value inside here. Okay, so that's how we can iterate through these lists. And there's many different use cases where you would want an array or a list of objects, and then you want to iterate through them. So, for example, maybe you're adding, maybe you have a list of objects that your user can put in, I don't know, they're naming their favorite foods or whatever. So you have this list of foods from your users. And then, let's say we can do an if statement in here and say if f equal equal cookies, then we can do something specifically because it's cookies. So we can print, we can print, I love cookies too. Okay, now what should happen? So what's, we have three objects, we're going to iterate through all of the three objects in here, but we only have one if statement. So if f equals equals, remember our if statements, if f, this is our variable, equals equals, that means is equal to cookies. So exactly cookies, then we print I love cookies too. So what would happen is, even though we're iterating through all three different things, we're only printing one line. And that is, I love cookies too, because we have cookies one time. Now we could go, I'll do an LF statement. So if LF equals tacos, let's make tacos tonight. So what we're doing here is, we have our if statement and I'm just using an else if on with my if statement, but you could just use another if statement, or you just could use, yeah, you could use another check if you wanted to. So what would happen is, I'll get two lines because we could have one that tacos is true the first time and then cookies is true the second time. So if you run this again, you get two different lines, let's make tacos tonight and I love cookies too. And that the order that they ran in was because tacos was first, and then cookies was second. So imagine that you had a list or you ask the user for some input, and then you evaluate all of the different things that your user listed, and then depending on what they input, you can change your response to whatever it is that you want to do. Okay. Um, right, so I'm going to remove these if statements, but that's basically what we can do is go through and then decide what to do based on the value inside the list or multiple values inside the list. Okay. So I'm going to get rid of the four statements. So what if we want to add something to the list? So let's say that as we're running our program, we stop and we ask the user for some input, which we'll show you how to do later. I might get an additional item that I want to add to this list. So we could add it just by doing food and remember food is our variable. And then we do dot append. And then we can append whatever value it is that we want to append to it. So let's say I want to add chips to the array. So I'm going to just do print food. Remember, I'm printing everything in the array. So here I will print food before we append and then print food after we append. So we can see the comparison. Okay. So before the append, we have tacos, cookies, kimchi. And then after the append, we have tacos, cookies, kimchi, chips. Notice I've appended this to the end. So anything that we append is going to be added to the end. Okay. We can also remove things from the array. We can do food.pop. And then we can specify the index that we want to pop. So let's say I want to remove cookies. Pop's useful for a lot of other things, but we'll talk about it later. Let's say I want to remove cookies from the list. So then we can do print food. So now we have three values inside our array. I print the three values, then we append chips to the end, and then I pop the value in index one. Okay. I don't really care that the value is cookies or not. Whatever is an index one, that's what I'm going to take out of the list. So let's run that. And here we have tacos, cookies, kimchi. That's what we start with. And then we appended chips to the end. And then we popped the value in index one. So cookies was removed. Okay. And we can pop any value we want. Let's do, if I do two, the next time we run this, the value in two is kimchi. So it's going to remove kimchi. So let's hear this out. And here kimchi is removed because we popped it out of index two. Okay. And then say that instead of popping, like we can either use the index and then choose the one we want to pop out, or we can just use dot remove and then remove anything that matches kimchi. Okay. So food dot remove kimchi targets kimchi. So it doesn't care about the index, just anywhere that kimchi exists, it will remove it from the list. So if we run it again, exactly the same thing because kimchi existed, we appended, and then we removed kimchi from the list. Okay. So that's really pretty much it for getting started on arrays. Just think of them as variables that contain multiple values. Then you can iterate through the entire array and then do something with it. So we use it for a lot of different cases. For example, testing whether all of these things are food, maybe we want to remove anything that's not actually a food in our list, or maybe classifying different types of food, like you can do a lot of different things with these listens are really powerful data structure to know. So I would recommend trying to create your own lists using length. So we had print and then the length of food. Use length to calculate how long a list is. Use for loops to iterate over the array. Something like that. And then try appending or adding things to the list and removing things from the list, either by index or by value. Okay. So this is an interesting data structure to try to get used to. So play around with it as much as you can. That's it for today. Thank you very much.