tubifier / example2.txt
wldmr's picture
app file
77eaf9b
Okay, so we've taken a look at loops and now we're going to just take a little bit of a look at some of the operations that you can do with loops. Python has this as we'll soon learn object-oriented approach to its operators. And the plus can add strings and it can add numbers. And floating point numbers, integer numbers, strings, etc. And so the plus similarly works this way with lists. The plus looks to its left and looks to its right and says, what am I adding? And in the case that I'm adding the list 1, 2, 3 and the list 4, 5, 6, it can catnates them together. And this way it sort of functions like a string. And so we get 1, 2, 3, 4, 5, 6. It just can catnate this list to another list. And it doesn't change a or b just like in any kind of assignment statement. It calculations on the right side don't change the variables and then produce a new variable and then assign that in to c. You can also use list slicing. And it's easy to remember if you remember how strings work, lists work exactly the same way. So of course it's a little tricky. The first number is the starting position. They start at 0. So 1 is right there. So it's the 0 position, the 1 position. Start at 1. But go up to but not including 3. There's 1, 2, 3. So this goes up to but not including 3. And that's why we get 41, 12 out of that. So up to but not including. I'll just say that over and over and over again. If we do you can leave the first part out, you can leave the first part out here and you can say, oh, up to but not including 4. So that starts at the beginning. Goes up to but not including 4. And so that's how we get that piece right there. We can say start at the position 3, 0, 1, 2, 3. Start at position 3 and go to the end. Now the fact that the number 3 is in here is sort of irrelevant. 3 to the end is those 3 numbers. And then you can do the whole list of slicing as well. Again, these are pretty much the exact same examples I used when I was doing strings. They're pretty much the same. There's a number of different methods and you can look up all the documentation and list. I often just use the Derr command to remind myself of them. A pen we'll look at, count looks for certain values in the list, extend ads, things to the end of the list, index looks things up in the list. Insert allows them the list to sort of be expanded in the middle. Pulls things off the top, removes an item in the middle, reverse flips the order of them and sort, puts them sorted order based on the values. So let's look at a couple of these. So if we build a list from scratch, we have a way to ask for an empty list. There are a couple of different ways to ask for an empty list. We could use just two square brackets next to each other. But this is a form we call the constructor form where we say, hey Python make a list. In this case, the word list is like a reserved word to Python. It's really a reserved class. But say list parentheses says make me an empty list and then assign that list into stuff. So stuff is now it's a list of object, it's a type list, but it has nothing in it. And then we can call the append method, stuff.append and stick book in. And then we say, oh, and that knows how long the stuff knows how long it is, where the end is and how to add something to it and then add a 99 to it. And we print it out. And we print it out. We got book in 99, reminding ourselves that lists while they're often the same types of variables, same types of values in the various positions in the list, it doesn't always have to be that way. Then we say, oh, we'll stuff that append cookie, you can keep on going and then we end up with three things and the cookie. We have an in operator, works pretty much like the in operator in a string, is nine in my list. And that's pretty simple and the answer of course is yes, nine is in my list. Is 15 in my list looking through? No, it's not. 15 is not in my list. And then there's the not in operator. Think of that as kind of like one operator. Is 20 not in the list and the answer since it's not there is true. And so that's a way to just, you know, it's kind of like starts with or infrastrangs, same kind of stuff. List are in order and they're sortable. And so this is something that we take good advantage of. A lot of computers want to do is sort stuff, you know, look all these things up, append them and then get them sorted. And so there is this method inside of, inside of list, that's just the sort method. So here we put three values in 012, position 012, Joseph Glenn and Sally. And then we tell the list to sort itself and then we print it out. Now this is actually sorted the list in place, which is different like then upper and lower because if you remember strings are not mutable, but lists are mutable. And so you say, hey, just sort yourself. Okay, and so just sort yourself and then it sorts it and then it's in alphabetic order, Glenn, Joseph and Sally. I happen to be clever. I only put strings in there and I put my uppercase in lowercase in a very consistent pattern. But the list is changed. And if I look at list sub one, that is the second item, which is Joseph, the print's out right down there. There's a whole bunch of built-in functions to help manipulate list. The other things I was showing was method, sort is a method that's part of list, but there are other functions that take list as their arguments. We already talked about the LEN function. Tells you how many items there are. There is pretty obvious max. It says go through and find the largest. Men go through and find the smallest. Some goes through, adds them all up. And we can say let's do average by taking the sum of all of them and dividing it by the length. And you might think to yourself, oh wow, I wish we didn't know this as few chapters back when we were having to write all those loops to do max, men, some, largest, smallest, etc. You can kind of think in your mind that inside each one of these functions is a loop that does pretty much what you did in those chapters. And part of the reason we did that back then, even though these things were here, was kind of easy loops to understand. And so those are there. And basically there allows two different ways of building loops to do the maximum minimum. Now it's not necessarily all that much easier to do something using these because you either can do them the old way or you can do make a list and then use these functions. So let's take a look and I'll just say that these two bits of code are doing the exact same thing. And what they are, is they're implementing a program that's going to repeatedly ask for numbers until we type the word done. And then it's going to compute the average and tell us what they are. And so using sort of the stuff from the loop chapter, we start with a total variable and account variable set them to zero. And then we read a number, we check for done, we have to break out, but then we convert it to a floating point value. And then we say total equals total plus value and count equals count plus one. And so this is going to run over and over and over again. However, many times we're going to do this and then it's going to pop out and when it's done, it's going to have this value of total. The running total will become the overall total divided by count and it'll print the average out. And so that's kind of how we would have done this before we knew how to do this with lists. Now let's take a look at the other one. In the other one, we say let's make an empty list. Remember this is that constructor syntax that says to Python make me an empty list and assign the empty list. It has nothing in it, right? But it is a list, has nothing in it into the variable num list. Now we're going to write another loop. We're going to, this part here is the same, these three lines, read the number if it's done, quit and convert it to value. But instead of doing the actual calculation right now, what we're going to do is just append it to the list. So the list will start out empty, then the three will be in the list, then the nine will be in the list, then the five will be in the list. So we're appending each time through the loop, we're appending into the list. So we're just growing the list every time I read a value instead of actually computing something with the value that we've got. So either in either case we get value, in one case we append it to the list. And then finally it finishes, the break happens, and then we just say, oh, hey Python, sum up everything in the list, add these three numbers together and then take the divided by the length of all those things and you'll have the average. And so these two things give us exactly the same output. Now there is one difference. If there was like one million or one billion numbers, they actually have to all be stored in the memory simultaneously. Whereas here it's actually doing the calculation of the billion numbers and not using up so much memory. For most of the things that you're going to be doing, the difference in memory, there is a difference in memory. This uses, this one here uses more memory, but I can't draw very well, more memory. I use the more memory, but it doesn't really matter by the time it's all said and done. And so for you, the difference between these things is not all that significant, but it's important to understand that there are just two techniques to accomplish the same thing with lists.