Hi, welcome to this new session. We are discussing how classes could be instantiated within our code. How can we differentiate each instance of a class, having different colors, different speeds and this time in this session, we're going to see how we can actually construct instances of a class through for loop structure. Using a kind of a loop structure, we will actually create a series, hundreds, potentially instances of a class and see how we can activate all of their functionality one at a time. Let's jump into the code. This is a code where we left off. Let's just do a quick recap where we left off, we actually have three instances of this gradient rectangle class. As you can see here, we are actually doing a very manual process of construction where we're explicitly declaring each instance and providing what you can see here in these lines are all the arguments that are part of the constructor of the class. All of them start in different positions, they actually have different size, different color, and different speeds. This is what we actually want to somehow automate. We want to just do it within a structure of a for loop. Let's just get rid of two of these instances, we're just going to keep one, and here as well, we are just going to get rid of two instances of that. Just before this, what we're actually going to do is create a list. We're going to first of all, add the data structure of the list, which is something that we covered in week 1. My list and this is going to be an empty list because we're going to try to store all these instances within a list. We can actually have classes within lists and this is our first version of seeing how that is done. Number of columns, it's going to be 50. We are going to define how big the population of objects in this case, it's going to be defined by this variable number of columns. Here, in the setup, we're going to execute a for loop, and let's do the for loop saying for i in range between zero and the number of columns. This is the number that we want to loop. What do we want to do here? We want to do the logic of the construction of a class or basically an instance of a class. We want to do that within the for loop, and we also want to add the result to the list. Every time we run this class we want to include it to the list. We have this is actually the same. I'm just cutting and pasting the construction of the class, exactly what we've done before and then after that, we could say that my list.append. Let's append the gradient rectangle that we created. That's basically the foundation of how we would do this. The problem is that we are actually going to create all the vector positions on top of each other, all of the rectangles with the same variables, all of them with the same color. Let's just create a few variable before we actually construct this class, instead of providing a hard-coded value, something that is explicit let's calculate what that value could be so that it's different for each instance of the class. Let's imagine that we want to create a series of rectangles that completely match the size of the screen. Let's just create a variable that it's going to represent a division between this number 600. Usually, we have been using variables for this. In the past, we've used something like image size y. In this case, if we want to do this correctly, I would just do that once more, so let's just do that image size y equals 600. I don't think we're going to use the x, but it's always good to have those declared as well. This is because we want to be able to do code that if for whatever reason, we change the size of the screen, the code will still work as intended. In the image size y, we can actually divide it by the number of columns. We're trying to get what would be the size of each rectangle, if we would divide the screen, the y direction of the screen by the total number of rectangles that we want to have. This variable would represent the size. All of them will have the same size, but they would actually be a division of the size of the screen. That's fine. Let's just start with the second we're going to call this the C positioning in y and the positioning in y will be the size times i. What we're doing here is using the iterator variable, the i variable that in the for loop will start with zero and get to 50, and we're iterating down. We're saying let's start at zero because multiplying anything by zero will be zero, and then we jump one times the size. We're actually offsetting by the size of the rectangle, how much down are we going to be pushing the next rectangle. We can use this variable. Now, let's imagine that the position of the rectangle we could say zero in x and c position in y. This is going to define the y position. The x is going to be on the left, like hard coded to be on the left side of the screen, but on the y we're going to be distributing these rectangles as we move down. Let's just create a variable for that as well. Let's call it random color and we've done this before. We can actually use a color, which is random.randrange between 0 and 255. That is for the green. We can do that. Three times. For the blue and green RGB, so red, green, blue, and we would have a random color that we could apply now instead of saying hard coated value of red, this one will be the color of each class. At this point, let's just leave this velocity vector at one for now. We're going to come back and randomize that as well. But I think that it's important that we start visualizing. In case we're running into errors, I always try to keep pressing play in my simulation so that I have a sense of where could that error be introduced in case I do have an error. This line is very important because we are appending our instance of the class to our list. When we get to the draw loop, in this area here, we know that we have to execute the functions, the display function and the move function of the instance. But we don't have that instance anymore because we're constructing it within the setup. So it's not visible, and it's not one instance. It's actually going to be a series of instances. It's going to be 50 instances. Here, we are also going to use a for loop structure to invoke the functions that are going to be executed. We're going to say for x in my list. Let's just spell it the same way. What is this type of for loop? This for loop that we're doing here is a specific kind of for loop that would allow us to select each entity inside the list. It's a much shorter for loop, so it's looping through a list where x is an instance or the object that we are going through so we don't need the integer i, we don't need anything like that and here we could say x dot display because the entity we're selecting will have, in fact, the function display, and it will also have the function move. This is a very neat short version of a loop that allows us to iterate over every instance of a class inside this list. We're calling that instance an x. Basically, you can put any name here but whatever you use here will be the variable name that you're giving to the instance, and we're invoking its functionality. In this case, display and move. Let's see if this actually works. We are having an error. This is not going forward. Let's just find where did we make our mistake. I think I have an idea of what this is. If you remember, we started using the random function, but we haven't imported. Let's import the random module. That could be one of the things that is broken. There we go. As you can see here, we have 50 instances of this class, all of them moving at the same speed from left to right and gradually painting the screen. Every time that we run this, we are going to get a different set of colors because each color is actually created randomly as well, at the moment of the creation of the class. The interesting thing here is that we could actually use different numbers here. If we use a number 20, you see that we're dividing the screen in 20 pieces. This is the map that we did somewhere here where we're basically saying the size of the rectangle, it's going to be a division between the total number of columns, which is 20 and the size of the screen. If you want to do something like 200 as well, that should work and you can see you can get very fine. We have 600 pixel so if you wanted one for every pixel, you could go up to 600 and that would get you a particularly interesting pattern. The final thing that I want to see here is that we are hard coding the value of velocity with a number 1. Let's just make that also just for the sake of creating a random value there, as well. We can use a random speed, which is going to be a random and here, I'm going to use uniform just because we want to have a random value, that it's a decimal place, a floating point value between 0.01 and 1. We don't want this necessarily to be too fast that way, we definitely wanted to have some variability in the decimal space between zero and one. The velocity vector, let's just add instead of a one here, the random speed and let's see how that looks. You can see here that we have 200 different instances of this rectangle all of them with different colors, and all of them moving at a different pace, at a different speed, if you want across the screen. We're actually starting to get something quite interesting, graphically, and we're doing it through the modularity of classes. We are actually able to create one class, we haven't changed much the class from last session, but we have been able to create hundreds of instances of this class within a for loop and then in the draw, we're just executing their functions that allow them to move and be displayed on the screen. We're going to leave it here and I'll see you in the next session.