n some kind of a loop really tight and it's running something and and it's using up all of the processing resources of your computer That's an infinite loop And so the problem is is we did nothing with the iteration variable Now here's a different loop and so this one Demonstrates a different idea. So in this case we start out with n is zero and it comes in here And is n greater than zero question mark and the answer is false So it skips it it doesn't run these lines of code at all And so this loop doesn't run at all because it comes in ask the question It says no and then it skips right around it So never run never run and so this actually is sometimes you write a while loop On purpose like this not quite as simple as this one But the idea is is this is this emphasizes that these loops are what we call zero trip They are not even guaranteed to one run run once they're they're going to run Maybe zero times and in this respect it functions exactly like an if statement Right being the first time through the loop if it's not true. It's just going to skip right by it So there's a couple of ways of getting out of loops in this case. I'm constructing an infinite loop because Remember the kind of definition of an infinite loop is if this is going to stay true Well true is the constant true So this is going to run forever and what it's going to do is it's going to prompt with a little Little arrow and then let us type and read whatever we type into the variable line And then if the line is done we're going to break now break is an executable statement And if you hit the break it exit exits the innermost loop out to the to the place beyond the the end of the loop So When this runs the first time And we say hello there Line is not done so it prints it so it prints out hello there and then goes up And then we type in again. We type finished and so it doesn't it's not done So it prints it so now comes that print statement Then we type in done and now this becomes true And it comes out and runs the code beyond the end of the loop The key is is it doesn't go back It's like once you've done a break that loop is done And so you so you look at basically You know the block that is the loop. So here's kind of the loop block And then the break goes to the line after the end of the loop block And you can think of this as sort of like just a hyperspace jump There is nothing really this could be literally hundreds of lines with if statements And you could be running and doing all kinds of stuff and running or doing all these things You know and these things could run all kinds of ways, right The point is is as soon as you hit a break statement however much stuff is down here However much stuff is up here it exits to whatever the next line is beyond the end of the loop Continue is another loop control statement, but it works differently than break So break says get out of this loop Continue effectively says Stop this iteration we're done with this iteration And so continue says go up back to the top of the loop. Oops. Yeah go up back to the top of the loop And so here we read a line if the first character is a pound sign Line sub zero if that first character is pound sign We're going to skip it And this is a way for us to make like little comments in our typing And then we print if the line is done we get out and otherwise we print it And so that's why there is no print out here because it comes in Runs oops It comes in This is true and that goes back up, but it comes back And prints out the next one and does another thing and so the loop continues Whereas the break ends the loop And so again the same kind of notion that you're sort of doing all kinds of complexity Wherever you're at in this loop You hit continue and it does not it doesn't go any further It goes back up and runs the question mark it asks the question mark And and so I mean ask the question and it might exit the loop in that particular case But this one here is a true This is an infinite loop that I've constructed This is not an infinite loop because at some point the break gets us out of the loop And so it's an infinite loop with break to escape it And that's another a common way to construct a loop So these loops that we've been drawing so far the ones that use while as their key keyword Are what are called indefinite loops And that's because they kind of go for a while till a break hits or until some value becomes true I mean until that as long as that value remains true So when we all the ones we've done so far easy to look at and know that They look pretty good and they're probably going to finish But there are sometimes if they're long and complex and and their exit or termination conditions Are a little more complex. We're not it's not clear that they're really going to terminate And so we we can use while loops for a lot of things but For most of our looping we're going to use what are called definite loops and that's what we're going to talk about next So definite loops use the for keyword and the idea of a definite loop is it's going to loop through some set of things It might be a set of lines in a file It might be a set of characters in a string. It might be a set of strings in a list of strings But whatever it is. It's sort of going to run A finite number of times depending on the thing that it's looping through and we like this and It it's an easier way to construct it And we actually don't have to deal with the iteration variable the for loop includes a mechanism to construct the iteration variable for us So it's definite loops iterate through the members of a set. So here's a very simple for loop And so you see the for keyword And in is also a keyword And the iteration variable is something we put right here this i is declared this i is like an assignment statement And i is going to take on successive values So i is going to be five the first time through the loop then i is going to be four the second time through the loop third To one so i is going to be assigned five different times to five different values And then the loop is going to run It's going to run once with five once with four once with three Once with two and once with one and so this block of code We have contracted say execute it five times with these values of i i is that iteration variable i is the thing changing through each iteration of the loop okay, and so That's why this prints out five four three two one and then when it's done it finishes it So this is a much more direct syntax for looping five times and setting an iteration variable you kind of all Combine it into this one thing right all into one thing. So it's it's quite nice So you don't have to be going through a list of numbers There's all kinds of things that we can iterate through with four and by the way while i'm sitting here don't I named my variable friends because that's a list of strings and friend Which is the iteration variable i'm using singular and plural because it helps you read it python doesn't understand singular and plural So just because you say friends doesn't mean python knows it's a list python does know it's a list But it doesn't know by the name of the variable i've chosen That's your basic mnemonic variable warning these are cool variable names But I don't want you to get confused by them So you can loop through a variable So we're going to take this list of three strings and stick it in friends And so friend is going to iterate through that so the first time through friend is going to be joseph Second time through it's going to be glenn third time through it's going to be sal And so that just says run this loop run this code the indented code three times Each time the variable friend takes on a successive version of a successive value That's in the friends array So it says happy birthday joseph glenn sally and then we come out of the loop and we print done so If we try to draw a picture of what this is really doing Um The for loop is actually doing a whole bunch of stuff that we would have to do With maybe separate statements in the while loop. Um First it decides how many times to run the loop. So it's answering the done question. Which way do we go? And it is also then moving eye ahead. It's managing the iteration variable if you go back to The it's initializing it too if you go back to the while loop We had n equals zero while n greater than zero n equals n minus one So we had like three lines to control the loop to manage the duration variable Oh, the for loop. We don't have to do that. And so that's all taken care of and so that basically says You know The for loop by you using a for loop. Are we done? No, we have five things to work Well set out of the first one run it We're not done because we got one more set it to the second one third one fourth one fifth one and now we're done And that is all handled in a single line of code And that includes the duration variable and the set of things through which we are going to iterate through I really like the word in um, it is Mathematically, I mean it reminds me of The set theory where you say this is a member of this set or the for each Math isn't important here, but if you do know math the vertical bar means such that Right as a member of this set and those that kind of stuff member of the set I'll erase the math stuff so we don't over math, but it's like For each of the values in the set five four three two one Run this loop setting the iteration variable i to the members of that set so in reminds me For those of us who are math oriented in reminds me of a really nice concept in mathematics, okay Now you could think of this as sort of this looping structure where The for loop and this is pretty much how it actually runs inside the computer, right? Where it initializes it i it runs this runs this thing five times and then executes That's one way to think about it You could also think about it any about it in a somewhat more abstract way And think of it as all we're really doing is we have a contract with python that says Eyes we're supposed to run this code five times and I supposed to be five four three two and one So you could imagine this might be what's going on the for loop sets i to five runs our code The for loop sets i to four runs our code the for loop sets i to three runs our code the for loop sets i to two Runs our code for loop sits i to one and runs our code all we know is our code was run five ran five times and By contract each successive time We're getting different value for i and the value for i is taken from this set And so this is just one way to think about it to say to yourself. Oh, yeah This is one way to think about it as it's actually and this is how it really works But this is also kind of logically the contract that python is making for us So up next we're going to talk about Taking this notion of doing something to a lot of items But accomplishing something with that and I call these loop idioms So now we're going to talk about loop idioms and loop idioms are patterns That have to do with how we construct loops. We have the mechanics of Fours and while's but ultimately we want to get something done We want to solve a problem with a loop and often what we have to do is If we have a set of things whether it's lines or strings or characters or numbers We're looking for something like the largest or the smallest or we want to add them up or something like that and so We can't just say add them up We have to say go through each one and do something to each one and somehow achieve adding them up And the pattern that we're going to follow is we're going to have this loop That's going to do all one run once for each thing Right in some chunk of data And then but we're going to set something at the beginning and then we're going to do something to each one And at the end we're going to kind of get the payoff. We're going to get the result So if we're doing sort of summing things We're going to have a running total and so this will be like t equals zero And then this will be t equals t plus the The thing value and then but this is not the real total. It's the running total during the loop But at the end it is the real total And so we're going to we're going to look at what you do at the before the loop starts during the loop and then What you get after the loop and how you can use that So we're going to use this loop It's just going to loop through a set of six numbers over and over and over again Right So we're going to do something before the loop or and do something after the loop and then we're going to run Loop some number of times and in this case thing is our iteration variable because I'm using unneumonic variables now So this is going to run, you know 9 41 12 3 74 and 15 So it's going to run and print these things out. So it runs this loop six times And away we go. Now this loop does nothing except print stuff out Of course, I like to do that first is always print things out To make sure that sort of my brain is as functioning So To kind of understand how these loops work I'm going to ask you to function as a program and I'm going to show you some numbers in succession And I want you to Mentally figure out what the largest number is but more importantly think about how your brain is solving this problem Of what is the largest number given that I'm only going to show them to you one at a time for a little while And your brain has to do something and imagine I was going to show you thousands of numbers I'm not but imagine I was how would you organize yourself in a way So that for like an hour and a half you could sit here as I showed you numbers and you keep track Of the largest number that you've seen of all the numbers. Okay, so here we go. Here's your first number second number third number fourth number fifth number Sixth and last number What was the largest number? Hmm, what was it? Well, it wasn't too hard It was 74 But that that's not the question.  So here's all the numbers if I've shown you all the numbers and asked you um What's the largest number your eyes would have sort of gone? And then you got to 74 and and you wouldn't do it in any particular order Your eyes would just like see the 74 and it would just throw smaller numbers away And it would move really quickly to what the answer is even if there was Several hundred numbers on the screen your mind would sort of move Fluidly wherever it felt like moving and then arrive at it And probably what it would do is it would do something like You know kind of move like this find this and then sort of check to make sure that it's okay And then so I like okay. I got 74. I'm done But that's not how computers do it. That is not how computers do it. They do not move fluidly But they are highly dedicated. They're going to do something 74 But how would you construct a loop to achieve this? So let's take a look You could create a variable called largest so far and this is the largest variable the value that you've seen in the list so far And I haven't shown you any numbers yet. So we'll just set this to negative one to get us started so now We see three and we're like, oh, that's better than negative one. It's our first number. So it's probably the largest we've seen so far, right Great 41. Oh, that's bigger than the largest we've seen so far. So we'll keep it 12 is not bigger than 41 So we're not going to keep it notice this keeping thing 9 is not bigger than 41. So there's no point to keeping it 74 is bigger than 41 So we'll keep it. Is this the largest number? We don't know. We don't know until we're done 15 not better than 74. So now we're all done and Hurray, hurray, hurray, we have the largest number And we had this variable that we kept the largest number that we'd seen up to this point And then when we know that we're done at the end Then that becomes the largest So if you look at all the numbers keeping track of the largest so far at the end of all the numbers The largest so far and the largest are the same thing. And so that's how you get this idea Of something you're doing during the loop is not really the answer But by the time the loop is done, you will have the answer And so here's a bit of code that does this use it with our numbers, right So let's take a look So I have this variable called largest so far. I set it to negative one before the loop Remember, there's a loop before and a loop after and loop in the middle before it's negative one So now the num Remember underscores are okay. That's my iteration variable If nine is greater than largest so far. Well, largest so far is negative one. So that's true So this code's going to run. So we're going to remember The new number. So this is nine and so nine ends up in largest so far and then print it out So largest so far is nine After we saw the number nine Then We do it again So now 41 comes in And is 41 greater than nine the answer is yes, it is. So we're going to run this code Copy 41 into nine 41 into largest so far and then print it out and largest so far is 41 After we saw the number 41 Now we're going to run the loop again with 12 Okay, and you get the idea I hope is 12 greater than 41 Which is the largest we've seen so far and the answer is no it is not so we skip So the largest so far stays 41 even though we saw 12 I mean we're sort of like ratcheting up, but we never ratchet back down So we run it again with three and 41 And we skip this and then the largest so far is 41 even though we just saw three And now we see 74 is 74 greater than 41. See we never are looking at all the numbers We're only looking at the window on the numbers of the current number that we're looking at So is 74 greater than 41 the answer is yes So we run this code and then we capture the 74 So we've seen we just saw 74 and it is the largest so far And then we run it again with 15 But 74 is our largest so far and so it skips So 74 remains largest so far After 15 and now we're finished because we just ran the last thing before loop takes care of everything And jumps to this print statement and says afterwards largest so far is 74, but at this point it's also The largest right so largest so far became largest when our loop finished So that sort of gives you this notion of how we construct You know something at the beginning Some kind of thing that we're going to do over and over and over again And then something at the end and we put some print statements in just so we can watch it and see what's going on So coming up next we're going to talk about Some more loop patterns some counting totaling averaging and finding the smallest number So now we're going to look at some more patterns of the different things We can do at the top of the loop in the middle of the loop and at the bottom of the loop And the first one we're going to do is counting now. We're going to take a look at the number of Something the number of things in our list now we could just inspect it and see six But you will have four loops like you're reading through a file Or you know scanning through some data and so the notion of counting But you have to assume that you don't really know, you know Dot dot dot dot dot that there's going to be a lot more than just six But for now we're just going to do six and we're going to count how many things that we see In this loop and the pattern is simple You set a variable zork to zero at the beginning We often call this variable count in mnemonic And now we're going to run this loop six times one two three four five six and each time through We're just going to add one to zork so zork start at zero then it goes one two three four five six And we're going to print it out. So, you know, we see the nine and zork is one See 41 zork is two Minute zork is 16 when we see the 15 four stops and we print out afterwards and this then is six is then The ultimate count that we got so that's very very simple the pattern is That set it to zero at the beginning add one to it And if you run that enough times then this is how many that you how many times that happened And in a sense it's how many times this line ran Right sometimes you put this in an if statement etc etc etc Okay Oops Now we can do the same thing to get a total And the way the total works is you compute a running total Of the number of the items that you've seen so far and at the end the running total an effect becomes the total Will you a better variable name for this would be like sum or total or something but zork i'll use zork again So you set zork to zero And it starts out the total we've seen so far is indeed zero And then we're going to run this one two three four five six times and thing is going to be the iteration variable It's going to take on the successive values and each time through we're just going to take our running total and add to it The thing we've seen so we see nine and the running total is nine We see 41 and then running total becomes 50 we see 12 The running total becomes 62 we get a three it becomes 65 we get 74 we running total is 139 How many more How many more are we going to see we don't know it could be a million could be one Oh, it's only one we get a 15 our running total is 154 And what's true at any moment here is the running total is right Up above what we've seen so far now when we're done the four loop quits for us and afterwards 154 is indeed the total so they're running total while we're in the loop At the end of the loop after the end of the loop we have the actual total So it's not very difficult to convert this to the average because we calculated the count And we've calculated the running total and now we're going to have the average by simply dividing those Okay, so Now this time I've used mnemonic variables Don't get confused by this mnemonic variables are just friendly names I chose for you to read the code easier. I am not communicating to python in any way by naming this count and sum, but Count and sum is nice Okay, so I set count to zero and sum to zero. Oh go back up I set count to zero and sum to zero at the beginning And the count is zero and the sum is zero and then i'm going to run this loop six times one two three four five six And each time value is the is the iteration variable I count every time I run the loop I count equals count plus one Sum equals sum plus value so I have a running count and a running total And they show up here one two three four five six and then the running total And then at some point the for loop, you know, we do the last one and the for loop jumps out and it divides Six hundred fifty four is the count and running total and then it divides the average sum over count Okay, so that's just again a pattern of something in the beginning something in the middle Something in the end Another kind of thing we tend to do in loops is we look for things we hunt for things And so this is where we have an if statement inside of a loop And of course I've created a silly simple thing In this code I am looking for Large values that are values that are greater than 20 and again, don't think of this is just six numbers But I'm looking for all the values and I'm going to print them out So, you know it says before it's going to run this nine The wealth nine is greater than 20. It's false. So it goes back up 41 True So it prints out 41 then goes back up 12 false Goes back up three Falls goes back up 74 true. So it runs this. So how comes that little print statement goes back up And then 15 is the last one and that's false It goes back up and the four says we're done and then we do afterwards and so this is just the notion of having an if statement in side of a for loop where we're sort of Picking or choosing or selecting or looking for something in a large set of things that we're looping through We can also say I want to know if a particular value is there And so we're going to use a Boolean variable and we've talked about integer variables like 142 And then floating point variables like 98.6 and then string variables like hello world That have quotes in them. This is a fourth type type a kind of variable It's called a Boolean variable and it only has two values. It has true and false As a matter of fact these if statements They return Boolean values value equal equal three. That is returning a true or a false based on the value of Value There's a new monoconfusion there, right? But I'm using so I'm going to make a variable called found and that's a decent name for a variable So don't get hung up on that and I'm going to initially say Found is going to indicate to me whether or not I found a three in my list And I'm going to start before the loop starts Let's say false because we haven't found anything yet. So found equals false And so at the beginning of the loop found is fall before the loop starts found is false And now we're going to run this loop a bunch of times 9 is that true? No skip. 41 is that true? Skip 12 skip All right, so 9 41 12 and found has remained false because we haven't done anything to it But now in comes a 3 and this becomes true. So it runs this code So found becomes true And then we print it and you'll notice that when we see a 3 we get true And then it runs again. We get 74. It's still false 15. It's still false Run run run quit and the residual afterwards Is true and in fact if you didn't know any of this and you don't print that out All you know is that afterwards we loop through all those things and we know that there was a 3 in there That's what we're doing. So we searched all of them. We checked for 3s when we found a 3 And you can see basically that you know The found remains false until it flips to true, but then there's nothing to set it back to false There's nothing in this loop that's going to set it back to false So once it sort of catches the 3 then it remains true for the rest of the loop and then it just finds its way out Now if you want to think about it for a moment ask yourself How might we make this loop more efficient by putting a statement right in here think Think about a way to once you found it and it's true There is sort of no reason to keep on going. So what would you put there to perhaps make this Loop to look for 3s just to tell you whether or not there was at least one 3 in there Um, how to make that more efficient just think about that okay, so Now let's look back at the largest value that we started out with right and so if you if you think about this Let's kind of give it a Sort of a rough rough look here Largest so far is our kind of like a running total, but it's our our hypothesis is the best large number And we have this if statement that says if the number we just see right now is greater than the largest so far then capture it Right take whatever number we saw and capture it. So when we see uh nine, it's better. We capture it We see a 41. It's better. We capture it. We don't capture this We don't capture this we capture the 74 and we don't capture the 15 and that's how we do it So you could think of this as better When when the number we're looking at is greater than our working hypothesis of the largest We grab it because it's better. So this this line right here is the grab line Grab it Okay So then the question is how would you modify this code? To teach it to find the smallest value in this list of numbers Think of it as you have a starting number You have a sort of what's better in this grabbing notion How could you do that? Take a look Okay, so let's take a look So let's let's do a couple things like the if if you look at this if statement that's better Well, it's better now if the number is less than so if the if then but then we should probably change this to be Smallest so far smallest so far Smallest so far smaller so far smaller so far smaller so far right Matter of fact, that's what this is. We've changed the word largest so far to smallest so far and We've changed the greater than to a less than Is that going to fix it? We'll give you a second to look at it pause if you need Is that going to fix it? Is that going to find our smallest number? The answer is Of course No, it's not So if we run this code So we set the smallest so far to negative one and it starts out negative one we run it And it's nine is nine less than negative one No, it's not so After we see a nine the small so far is negative one now we're going to run 41 It's 41 less than negative one No, it is not So small so far still negative one as a matter of fact It isn't the smallest so far anymore just because we named it smallest so far doesn't mean it is the smallest so far It didn't work out so well and so you see that None of these because they're never less than negative one do anything and we claim that afterwards the smallest we've seen so far Is negative one and that is because of course Negative one is smaller than any of the numbers that we saw So how could we fix this? Well If we started the smallest so far with some like arbitrary big number then it'd be better So if we made this 100 whoops come back If we made this be like 100 That'd be good because the first time through the nine would be less than 100 So we would capture the nine and then the rest of the loop would work just fine But then what if we didn't know that how big these numbers were as a matter of fact The largest so far wouldn't have worked if all the numbers were negative think about that We just assumed they were positive and so we kind of wrote lazy code that assumed all numbers are positive That might not be a good assumption depending on the numbers that you're dealing with right So maybe a hundred's a good number to start with or maybe like a thousand Or 10,000 or like some number with lots of zeros in it. Well, how big should we make this? And the answer is we're kind of Solving this problem the wrong way And the thing we really want to do to solve the problem is To just accept the fact that if we're looking for the smallest number so far That the right Hypothesis is the first number And if we just knew what that first number was the nine that would either that would because it's the first number We know that It's the both the largest so far and the smallest so far as soon as you see the first number But we don't know Here before the loop starts what that first number is I mean you can look at it But assume this is just data that's coming from somewhere else and we don't know it until we start reading it So we have to construct a loop that deals with the fact that we want to capture the first value is our hypothesis for smallest so far So how do we do that? Let's take a look So what we do Is we use yet another type? So we have integer floating point string boolean and now we have a thing called the nun type nun type Is a special marker in that it only has one value boolean has true and false You know floating point has an infinite number of values and integer has an infinite number of values But nun type has one value nun nun is a constant capital nun is a constant The difference is as we can check to see if we have stored nun none is often used to indicate emptiness not Non-existence because because smallest doesn't exist until we assign it but we're going to assign it to like a mark A flag a marker some way to say oh, this is not even a number. It's nothing And so we're going to win you can do this. So that's like makes a variable called smallest And in it puts none It sticks it right in it's not a string. None. It's like a special type Okay, so that Actually captures the notion that before the loop starts the smallest number that we've seen so far is None. We haven't seen any numbers Okay, so Then we come in And we have an if statement and we have a new operator called is Is is stronger than equal sign And so if smallest is none That becomes true It runs this case And so then what it does is it copies this first value which is nine Into smallest and so we see a nine and the smallest so far is nine, which is the first value And again, we're assuming we don't know what the first value is before the loop starts So we use the first iteration through the loop as the moment where we capture that. Okay so Smallest is is the value and then we print it and we go back up and now it runs again With 41 41 is not none none is there's only one thing that's none. So it is not equal to none Smallest is not equal to none or is not none. So this is false. So it skips over here Then it asks the question is the value we're looking at 41 less than smallest Well smallest is nine in this case and this is 41. So that's false. So it skips that and goes on So we see 41 we don't take it and then you can see that this will never become true again This is pretty much false for the rest of the iterations of the loop That's false for the rest of the iterations for the loop So it just is going to run down here and ask this question And at some point we see a three and we run this code. We capture it. We see 74 We don't capture it. We see 15. We don't capture it So then the for loop skips out at the end We have the smallest and actually this would be a good technique for the largest as well Because it really is just a technique to put a marker in this variable So that we snag that first number or first whatever as we Read and parse through them So there is and is not operators are very useful in python You can think of them as like the double equal sign. They're asking a question And They're asking a question and they return a true or you know blank is blank returns a true or false. It is stronger Double equal says are these two can these are these things equal in type and value? So just as an example If I were to say Is zero equal to zero point zero It would say yeah, that's true But then if I says zero is zero point zero that would be false So that's because these two are the same value Wise and these two are not the same type wise. So is is stronger than equals meaning that it demands Equality in both the type of the variable and the value of the variable and no conversion is done And so that's just a very strong don't overuse is if you're dealing with numbers or even strings use double equals don't use is because Sometimes it it gets a little confusing. So use is sparingly I tend to only use is on booleans And on none types. I don't use is on integers and I don't use in Use is on floats and I don't use is on strings just none or true false And also is not is also an operator. So you just like blah blah blah is not none or blah blah is not false Okay, so we've uh been looping around and doing loops and loops of loops We looked at the uh the the indefinite loops the while loops that kind of run for a while The definite loop and we looked at break and continue as a way to either escape completely from the loop or Go back up and discard the current iteration of the loop We looked at none. We looked at boolean variables with four loops definite loops where you've got some kind of a set or a list or Some kind of sequence that you're looping through and then the concept of loop idioms where you do something at the top Something to each item and then something you sort of get a benefit at the bottom And uh and so that gets us through iterations Hello and welcome to chapter six and this chapter we're going to talk about strings and Chapter seven is the payoff chapter. So we you know up to this point We're still learning sort of basic building blocks and actually We're going to write a real program in chapter seven. So just learn this and the payoffs in chapter seven So we actually been using strings from the very first lecture because if you print hello world, well, that's a string And so we've been doing things this this little this slide here is all review Uh, we use plastic and catnate strings. We use print to print them out Prints just a function that takes as a parameter something strings integers, etc Um, we we we can put digits in strings, but we can't add to them By now you figure this out, but you can use things like ints to convert the strings To integers and then print things out. So, you know, we we've been doing this for a while We've been talking about strings all along now today What we're going to do is going to just get into Strings in more detail we reading we input data With the input function input returns us a string Um, and if we want to input a number we have to run some kind of conversion like we have to do on int Before we take this data that we read from input, you know And so there's there's things that we've got to do and we've been doing all these things in in programs so far But if we look a little in a little more detail inside strings, we can uh index within strings each character So each character has a separate position and a separate index And they basically the the letters are have positions and the positions start at zero And the best way to I explain this to remember this is it's the Elevators as we used in one of our examples a long time ago Elevators in Europe start at zero and so strings start at zero as well Turns out in the old days, there's some efficiency with the notion of Lists of things starting with zero these days the efficiency isn't the issue But there's a certain elegance Starting at zero even though in intellectually you might think one would be the the first Character in the string might make most sense to be sub one, but it's not it's sub zero, but just remember that um And so we have this operator called the index operator and it's square brackets. So You know fruit is a variable That contains the string banana And then fruit sub one is the character that's in position one now that actually is the second character I'll keep reminding you until I get tired of reminding you. So that that assigns a the the letter a into um I mean a the letter a into the variable letter, of course, that's a badly choice It's a either a well-chosen variable name or a badly chosen variable name Um and the thing that goes inside this can either be a constant or it can be an expression So this is x equals three and then fruit sub x minus one well that means two Which is position two which is an n and so that gives us An n so the index is an operator and you can add this bracket syntax to the end of a string variable You can't uh index beyond the length of the string. So if I say zot sub five Well, there's only three characters, which means zero one two But sub five doesn't doesn't work and of course we get a happy little trace back Mm-hmm. So you have to be careful when you're starting to pull stuff out of strings Although some of the things allow it some of them don't and then you'll kind of get used to that We can ask how long a string is And so we use the len function we pass the string variable And we pass it into len as parameter and len gives us back the length of the string not the position So it's zero through len minus one So it's zero through length minus one So length is just another function That we've been doing functions now for a while you pass in a parameter And then len does some work an outcome six and that goes back into x because the function has a residual value It just happens to be a built-in function And so, you know somewhere deep inside python There is code that takes this and somebody wrote a loop or look something up and then return to return value And sent back a six to go into our x variable And so function is there and like I said, we've been using this for a while Another thing we tend to do is to look through strings and look at strings and dig data out of strings Python is excellent for doing sort of these kinds of lookups And so we can write a simple loop. We can write a for loop that Creates some kind of a iteration variable like index And given that we know that these positions are zero through five We can set this to be zero and then write a while loop while the iteration variable is less than the length of fruit And remember this is six. So it's going to be zero through five Zero through five are the the very values we want to generate And then we can look up one at a time pull out fruit sub index So fruit sub zero fruit sub one two three four five And then print out the position and the letter index and then add one to index and it runs This will run six times zero through five and now we go to produce this output right here And so that's one way of looping through strings. That is a basic Indeterminate loop, but we construct carefully an iteration value Construct an iteration value and Work our way through that loop data The other way is to use a determinant loop a for loop and Generally when we are able to use a while loop or a for loop all else being equal We generally prefer a for loop and so here we have the for keyword and fruit and it's an in And so for letter in fruit. Well, that just says letter is our iteration variable And it's going to take on the successive values of each of the characters So this loop is going to run six times and letter is going to be b a and a and a banana I'm always terrified when I make these slides that I'm going to miss bell banana because somehow I always think that there are two ends Somewhere, I don't know It's not one of my favorite words to spell I actually didn't choose banana as the constant the author who I borrowed the textbook from Alan Downey and Jeff Elkner They used banana and so I'm still using banana So some of the jokes in the book aren't my book aren't my jokes. They are the jokes of Jeff and Alan So here are just two Equivalent, you know, so you can have the while loop. They sort of both do the same thing They both just print the letters out one one time through each of these loops runs five times But you can see how the the determinant loop the for loop is a prettier loop Unless you truly somehow need to know this number as you're going through the loop But if all you're doing is going through and you want to touch in order each of the characters of the string You then simply write a for loop because it's more elegant the less code you write The less code you write the less chance there is for you to make a mistake And so the fact that these are equivalent This is three lines that well two lines of a loop and this is four lines of a loop That's twice as many places as you could make a mistake because you might you know Misspell index or something. I mean why even make an iteration variable if you don't need to make an iteration variable And so we can do things that Harken back to our iterations and loops chapter Where anything that you can do in those things like look for the largest letter look for the smallest letter search to see if a letter exists or say Count the number of a's in the word banana and so that's what this is doing And so we um, so so we have a counter So again, we do something at the top of the loop We're going to do something in the middle loop and then we're going to print it out at the bottom So we start our counter at zero We're going to loop through b a all the letters and then if the letter is a then count equals count plus one This is kind of a pattern in a loop where we're noticing something and instead of like we did it earlier Where we said found equals true. Well, we're going to count them this time So if we have one we'll get one if we have zero we get zero and how many ever there are but there should be three Because it's going to run three times and there's three a's in banana And so this is uh, you know a conditional within count. We've seen counts. We've seen conditionals in loop In prior chapters and so again, I love the in keyword in python It again reminds me of a set notation in algebra If you're uh, if you're a math whiz if you're not don't worry about it or maybe you will be a math whiz And you'll say well this set notation reminds me a lot of the the in uh keyword in python So um Again, it's for iteration variable letter again, don't get stuck with letter I just happen to be using it here in banana and that is for Each character in the string banana Run this loop once Changing the variable letter to be the particular character that we're pointing at and so it's taking care for is taking care Of a lot for us, right?  I am now done Because I whoop You know, we're done with that particular string and so You can think of the four as you know, magically Doing all of this for you of both deciding how long to run the loop when you're done or not And moving down through all the success of letters in the loop So next we'll do talk a little bit about additional things that we can do with strings So now we're going to dig into strings a bit and we've already looked at how you can pull out a single character in a string And now we're going to look at what we call slicing and that is pulling chunks of a string out and again, we're going to use the square bracket operator and uh, and so so s The way I say it is sub s sub zero through four. That's how I read this s sub zero through four So I look at the colon as through I look at the brackets as sub And so uh, s sub zero through four says start at position zero And then go up through but not including four right so we don't include four So that's probably the hardest part of this up to but not including up to but not including This seems counterintuitive kind of like starting at zero seems counterintuitive But after a while You'll kind of you get used to it and there'll be situations where you're writing code like oh, that's why that works better But just for now remember it up to but not including it's just kind of a little thing We'll we'll come back to when that is useful for us Six through seven well that ends up being starting at six up to but not including seven So that's why we only get the p out Um now one thing that python is pretty nice about is it's not going to give you a trace back We might expect that six through 20 well, there's no 20 characters, but it's like ah That's okay. We'll just let you stop at the end and we'll start at six and go all the way to the end Oh, no trace back. It's almost disappointing sometimes when python, uh, doesn't trace back when you think You know, if you're so obsessed about everything, I would have traced back in that situation But hey, it's I guess if you're you're allowed you're allowed and so there we go Now you can eliminate or omit the first or last If you eliminate the first it assumes the beginning of string if you eliminate this eliminate the second it assumes the end of the string And why you would do this? I don't know but that's from beginning to end So it's the whole string so whole string eight through the end Is thon and up two but not including two Is moe right so so you get that so just it's that's pretty simple Once you got the rest of slicing and the rest of string indexing The notion of eliminating the first or the last between of the colon expression The set first or second of the colon expression. I think is actually pretty intuitive pretty nice We've already been concatenating strings together. We overload the plus operator And there is no space added. Remember when you're doing print x comma y This comma does turn into a space But that's not what's happening here. There is no automatic space being added And so we see hello in there and it just is hello there with no space And so if we want we just have to concatenate the space explicitly if we want to put spaces into strings The problem is is if you if this you might think it's more convenient to add a space with concatenation But then you have to think well, what about if I want to concatenate things and not put the space in then I'd need a different operator So that's kind of why it works that way We can use in differently as a logical operator So we're using it in as an iteration Structure in for loops, but we can also use it as a logical operator In if statements, so it's kind of like the you know, double equals or not equals or less than or equals or something like that It's it's like those guys and um, and so And it returns a true or a false is n in fruit So that's a question and the answer is true is m in fruit No, that's the answer to a question is nan in fruit doesn't have to be single character can be more than one character And the answer is true And then you say something like you know if a in fruit and so this is the logical value that returns a true or a false And yes, we found it So that becomes true in this particular case. So it runs the little indented bit So in is an operator in this particular situation in a for loop in means something different And we'll use in for other things as operators as logical operators coming up in a bit You can compare strings and this has to do with the character set of your computer the character set that python is But in general, um You know, it is lexographically less than and lexographically greater than uppercase and lowercase are a little weird Um, I think when we used the max function earlier the way my computer was set up Um uppercase was less than lowercase but In general uppercase is less than lowercase But in general it's it's bad to assume case. Um, but there is a deterministic way to sort strings You can you know have something equal to or less than or greater than And all those operations work Naturally the less than and greater than you have to kind of be aware of uppercase lowercase things like where You know punctuation sorts less than less than or greater than letters It's that's kind of unpredictable and depends on the character set of your computer and something you just play with and Figure out if you're doing sorting stuff by first name and last name as long as the case is kind of the same um, you know if um If you were sorting chuck with an uppercase and glenn The fact that these uppercases they'd sort right and these lowercases would sort right But if you were to subdue instead um lowercase chuck An uppercase glenn Then that would sort weird as a matter of fact this the g would come before that and so case can mess this up But in general other than case and special characters and other things It technically works. Um, it's just hard to kind of predict it A lot of what we do is use the string library and so the strings are Objects and we'll talk later about what that really means And objects have these things we call methods So a string object has some built-in capabilities And one of the built-in capabilities that the string object has is Here is a string object and because greet is a string object if we said type we'd see that it was an str Dot lower says hey dear string Make a lowercase version of yourself. It's like calling this function lower and passing greet into it And then give me that back to me now. It doesn't actually change greet. It gives me a lowercase copy So here I have hello bob with an h and a b uppercase and what I get back in zap is hello bob all lowercase And note that greet is unchanged. So hello bob is still there And you can even call these methods on constants. So this is a string object quote high there quote dot lower that says Call lower on this bit of string and give me back a lowercase version of it And so it prints out as the residual return value This is like a function call a method call is a kind of special form of a function call It's a function call where you say the thing dot the function name rather than Function name passed in as a parameter like len for example is non object oriented You know len of x that's not object oriented object oriented to be x dot something parentheses But you so constants are objects as well and taking the lower gives us back lowercase high there And so that's just one of the things that you can do in the string library These are built into string variables and constants. They're just always there as soon as you make a string They're part of it and when you do type and it says it's class str We'll get to object oriented. Don't worry. We'll get to object oriented Okay, and so you can do things like use the type Um, if you're just look this used to say um type stir, but it's class stir Kind of this is more of an oh the word class as an object oriented concept But it is a string and you can use the dir and of course there's extra stuff up here And this is showing all the different methods Or capabilities things we can do to strings. So, you know x dot Something parentheses. Well, what can we do there? This is all of those things that we can do to x's that are that are built in and come with x's I mean come with strings Uh when we build them And python, of course has great documentation online for all of these string methods and what they do and how they work And why they work the way they do And so here's some of that python documentation. We'll look at a few of these Um, but you know, don't hesitate to say python string uppercase and then we're like, oh, yeah Yeah, that is upper Right and so here's a few things that we can Do and use some of the ones I use a lot and we'll look at each one of these things um So the find operation Says find me a substring within a string Right find me a substring within a string. So find me the first na and give me back the position So that gives me back two And then I can say go find a z in there. Well, there's no z and so it returns being negative one So that's what the find does. So we're do a lot and we're going to use this kind of stuff a lot And we do a lot of looking in strings Um converting things to upper or lower case. There is an upper method and a lower method So greet greet dot upper and that means the uppercase Nnn is hello bob greet dot lower that means that dub dub dub is the lowercase hello world and greet is Unchanged greet is still hello bob with upper and lower Because each of these methods basically say i'm going to give you back a Upper case copy or a lowercase copy of the original thing without changing the original thing Search and replace is super useful super duper useful And it's pretty clean Here we have a string and we use the replace method in this case. We're passing in the old and the new Bob replace all bobs with janes. And so that takes this hello bob and turns it to hello jane Again greet is unchanged Greet is unchanged Um, and it does more than one thing. So this says go find Well, let's clear that this says go find all the os and replace all the os with x's And so it goes and finds two of them and then outcome to x's And so that really is a replace is not just replace the first one, but replace all of them White space as we'll see is a big deal And white space is not just blanks although the most common thing But it's also sort of non printing characters like tabs and new lines and other kinds of things And so we have a number of different ways to strip white space Um, so here we've got some spaces at the beginning and spaces at the end And we print out we do an l strip and that throws away the spaces at the beginning. That's the left So that's a left strip. It all takes any if there's nothing there. It doesn't harm it Our strip means throw away all the blanks on the far end and then strip says go take Take both sides both sides for strip. And so that pulls out all the spaces on both sides This will be useful because sometimes when you're tearing stuff apart you find yourself getting extra spaces Sometimes at the beginning sometimes at the end and it can be tab or new line It's it's sort of White space space that um is kind of not visible clear.  It's like x. Well, that's not white space But right here. Oh, that's white space. It's any character that doesn't cause printing to happen If that makes any sense It's any character where nothing would be printed and there are characters like that There's like even bell characters, but we don't use them very much We can ask very conveniently. We can say hey does this line start with a particular string And so, you know line does this that's this is a question going to return a true or false Does this line start with please and the answer is true. It does start with please Does this line start with a lower case p? No, it does not and so again you use this in the context of if something colon some block of text some block of code So we can combine these things to tear stuff out And so let's assume that what we want to do in this case is we want to take a from line This is from an email form email format from a mailbox um And this has got the from with a space And the person's email and then at sign in the school they're from And a space and then the rest of the stuff like when this mail was sent And this is a real mail message from this guy steven from the university capetown in south africa It's really steven and this really is the first line of a file that you'll get to know pretty well by the rest of this course Hi, steven You we like you You are the example in my class and have been for a long time People actually who know steven have taken this class and they're like steven I saw your picture in the class So if you're ever in capetown at the university capetown say hi to steven and tell them that you saw him in the class But okay, that's neither here nor there What I really want to do is I want to extract his school from this email line okay, so Now eventually we will do things like you look the data will come from files But this is still chapter six. So this is the data. We're going to search through and so we can say hey Let's go find the at sign Search up to this position and find the at sign. So data dot find at sign and give me back where that's at that's in position 21 position zero then What we're going to do is we're going to look for the next space after the at sign So we're going to start at the at sign until find to start here and look forward until it finds a space So data dot find look for a space starting at the position of the at sign And then that'll be in position 31. So 31 is what we get in the space position So now what we have is we have in two variables. We have Position the position of the at sign and the position of the space after the at sign now What we really want is this bit right here. So we have to go one beyond the at sign And we don't want the space. So we say we're going to use slicing here data sub At position plus one up to but not including the space Oh smiley face because we didn't have to say space minus one because that is up to But not including And so we get that little bit right there So we don't have to this we don't have to say minus one there Because this is not actually included the thing that's at the position the space is not included So that's already a little benefit for the up to but not including and so when we print this variable out host We get exactly just the school that's steven Works at and probably went to as a matter of fact I don't know if you went there or not. So this is just kind of a note um For uh non latin character sets, uh, you know all programming languages from the 60s on tended to Work in what we call the latin character set, which is united states and england and europe and lots of places use this abc Character set and the special characters, but it's really common to want to Use different characters and so if you're going from python 2 to python 3 And we'll talk about this a little later when it matters more um, luckily we're in python 3 and so Python one of the big things about python 3 is that all the internal strings are unicode in the in python 2 There was sort of some confusion as you went between strings and this is just a little bit of code And so i'm putting a in here You know some uh aging characters. This is korean actually Asian characters into x and i say what kind of a thing this is and that is Uh a string and then there's this unicode and this comes from python 2 If it's a unicode operation It's still a string whereas in python 2 if you put a International characters into x then it was a string and then there was a separate kind of a constant called a unicode constant And it was a different type and there was ways that you had to Mess with these unicode variables As you did things like read them from files and put them back into files and did other things So it was much more difficult In python 2, but we're doing in python 3 and in python 3 it natively understands non-latin character sets international asian character sets Spanish french character sets And so this is a good thing for python 3 and this is one of the real benefits of using python 3 And as we start doing stuff where we're exchanging data with the outside world This will come into play and I'll have to show you how to use it Um, there was weird things that you had to do. It just makes a lot more sense in python 3. Okay So we've talked about strings. We learned about the string or converting it We've done a whole bunch of stuff and this is again You know, we're not we're not yet doing anything super useful We're learning sort of how to like slice and dice even though we're sort of not making the meal yet Up next we're going to talk about files. We're going to read some data And we're going to slice and dice and use all the things In the next chapter that we've learned up to this point. So see in that. See you in a bit Hello and welcome to chapter 7 This is the chapter where it all really starts to pay off. We have been learning bits and pieces And doing little Two lines three lines four lines of code to learn the basic building blocks of python and learn some of the syntax And find lots of terms, but now we're actually going to start doing something So if you look at what we've been doing so far You know, we have been We are inside this little computer and you type up you, you know, the python says what next and you give it its command And it does something and you do something else and does something and you do this three or four times Unless you write a loop and then it goes like, you know, 10 20 times and that's it And then maybe we write a thing that reads something from our keyboard gives us something back And then we write something to print something out print a few foot things out And so we've been pretty much using the keyboard the screen The cpu and the memory that's kind of where we've been living And while it's important to talk to the keyboard on the screen The the real world is things like databases that live out here Files live on our systems and you know connecting to the network and reading Reading data from the network. And so that's what we're starting to do right now is we're starting to Be able to work outside kind of our code and create things that are permanent And so we're going to be talking initially we're going to work on files We'll later talk to databases and the network and other stuff But for now we are talking about files and so really kind of We're stepping out a little bit and creating reading things that are prominent and creating things that are permanent The kinds of files that we're going to talk about mostly are text files and you can think of these As a sequence of lines in a file that are easily read by python Um, you you've been making text files all along. You're you know hello dot pi That file is a text file too. You're using a text editor to create that file You put your python commands in a file you run those files And that's what it is and so a file can be thought of as a bunch of lines You know of one two three four five six seven a blank line here. That's possible and um But the but the reality is is that these are actually just lines And we have a special character called the new line that we'll talk about in a second So to read a file, uh, you have to call the open function And open returns what we call a file handle open doesn't actually read the file open makes it possible So that you can read the file So the the parameters to open are it takes uh one parameter It's required which is the name of the file another parameter It's optional whether not to read it or write it if we're reading the file It doesn't harm it you can read it over and over if you write it It actually if there's already data in that file it truncates it and write something And we're not going to really write files We're mostly going to read them and so open sort of you pass it in a file It gives you back this file handle and then you have a variable in which you store it I often call it f hand to be mnemonic Just you'll see my code. I use f hand all the time to indicate that that is a file handle And so if we were to run this in, uh An interactive mode will open mbox.txt And that is a function built into python and then it gives us back a handle Does not give the data and kind of see this When we print out the file handle using the print statement It doesn't print the lines that are in the file the lines that are in the file are sort of out there There could be like, you know, 10 million lines for all we know lines in the file Handles like a little opening outside of your program And you can talk to the file by opening it then you can read stuff You could if you're writing the file you can write stuff And then you close the file to shut the handle down But handle is a thing that allows you to get to the file It is not the file itself and it's not the data in the file It's just a Rapper that kind of allows you so this if you print it out. It's like that's the file we open We're reading it and encoding has to do with the different kinds of character sets Which we talked about at the end of last lecture that unicode character set, etc UTF-8 is a great character set. It's it's Probably the most typical character set that you will run into a although you can have Different character sets of files, but most of them are UTF-8 So of course, this is python if you make a mistake and there's a file that doesn't exist We get a trace back and it blows up um We'll show you how in a second how to deal with that now The new line character is an important part of file reading And in files and strings we can put the new line character in by this backslash n character And the backslash n is the character that indicates that we're supposed to go to another line Go to a new line Go to a new line and so we have what is this? Well, that's a backslash n. That's a backslash n And so if we print it out we print it this way we see that the backslash n is in there This is how we type it we actually type backslash n to python to indicate that we're supposed to put that there um But if we do a print statement it actually interprets the backslash n And so the backslash n causes kind of this movement to the beginning now the print actually At the end of this adds another backslash n so so the backslash n that we put in by putting it into this string Is that one and then print always puts a backslash n at the end There's actually a way to override that backslash n behavior by putting something on the print statement Which we'll talk about later now It's important to note that the backslash n is one character, right? And so even though this x backslash n y prints this And then print adds another new line to go down to here if you ask how many characters the what is the length of this Well, it's only three That's because that's a character the backslash n's a character and the y is a character So it's a three character string. So the backslash n is a character like all the rest of the characters but it's only We we encode it by typing backslash n It's called an escape where the backslash is the escape backslash n is a way to say new line Because we can't see it. It's a way for us to encode in a string this non-printable character this invisible character The white space it's part of white space So as we're reading through the file We can think of it as a sequence of lines and we can read these a line at a time We can also read them a character at a time if we want and so but it's more common to say read this line Read the next line read the line after that etc etc etc But the way to best think about this it it it doesn't really matter You can think about it as lines and we will and most of the programs that we write But realize that the way when we see this um We see it like this It comes back to the beginning it comes back to the being there's a character in the file At each of these points to say go back to the beginning It's like hitting the enter key on your computer and that is a new line So you have to think that in the file in order for your text editor and python and everybody to know where the lines end You put new lines in the file and that's another character So You know this looks like an empty line This line here looks like an empty line But really it has a single character and the character is a new line And it turns out that in a bit we're going to need to keep track of the fact that Every line is ended by a new line So up next i'm going to talk a little bit about how to read files in python So we're going to find that there's a number of different ways that we can read through the file But the most common way that we're going to read through the file is to treat it as a sequence of lines And we're going to use the determinant loop the for loop To do this and so what happens here is we get back this handle That opens the file and gives us back the handle that handle x file Is the variable i named a call i just named it x file That's not the data, but it is a sequence It is that file handle represents to python a sequence that we can Potentially walk through and then get all the lines And it's the simplest most beautiful elegant way to read all the lines in a file We use the for loop and we have an iteration variable This is going to take when we talk about the file Cheese is going to be the first line then the second line and third line then the fourth line So it's it's like going through a string But you're going through a file now and you're getting it line by line So that's each line. I just picked a variable named cheese. So you didn't get confused later. I'll call this line But but python doesn't know anything special by naming that variable line Okay, and so this is it's the four and the in and so so for I read this as for each line In the file x the file handle x file So run this loop one time for every line and then print it out. So it's actually really quite simple Okay, um other languages like c or c++ or other languages They have to write while loops with end-of-file conditions and all kinds of things that make this very difficult But this is one of the prettiest things that python has. It's uh, it's a very very pretty thing Okay, so let's talk about what we might do and we're going kind of back to iterations now What if we wanted to count the number of lines in a file? Well, this is a basic loop counting pattern So we open the file and then like in all these loops We do something to sort of prime the loop to get it started set a variable count to zero And I'm going to use the variable line that's going to go through each of the lines in the file Four line in f-hand down the file and this going to run this loop once for each line in the file And the variable line is going to change But all I'm going to do is add count equals count plus one and so that's just like from counters That's just how you detect. So every time we see a line. We're just going to add one to the counter We're not printing the line. We're not even looking at its data at this point And then when the line is done however many times it has to go out it comes and we print out line count equals count And so if we open mbox.txt This is going to do all this work and then print this line out and say line count is 132,045 So this is a little five line program that shows you how to count the lines in a text file using python again simple and elegant and not too much syntax for you to have to learn Now it's also possible to read the file as a series of characters all in one go Read the whole file it now. You got to be careful depending on the size of the file This is going to lead to a string variable with a lot of data in it now if it's you know 100,000 characters that's actually kind of a small thing But if it was uh, you know 10 million lines that would probably not be good You'd want to read it one line at a time and process each line and then do something But mbox short.txt is a small little file So we open it And we get back a file object file handle object And we call the read method and that says look go through and read all the text and give it back in one big blob One big string and I'll put it in imp And so that's where you have a line a new line a line a new line a line a new line So not really lines. It's just a sequence of characters with new lines in there to punctuate them And now you can split that later. We'll see how to split that Into separate lines if you want now I picked a file that was short And so this imp variable now has a string in it and I can use the lend function Pass a string into the lend function. It says oh 94,626 characters. That's kind of a small A small little file and perfectly okay to read it all in one go And so now I say just print the first 20 characters That's you know beginning to up to but not including 20 and so it shows the the first 20 characters of that little file is a front line because this is a mailbox file Now let's say we're going to do a searching and we did this loop where you're looking for something And so we're going to search for lines that have a prefix of from Okay, that's what we're going to do and we're going to print those lines out So there's lots of lines in this file You know line line line line from Line line line line from right on and on and on and we don't we only want to show these lines the ones that match right? That's what we want to do and so We are going to write an open statement and then we're going to loop through And we're going to ask the question if the line starts with from print it So sometimes it's going to skip skip skip skip and that's going to run it and skip skip skip skip skip It's going to run it skip skip skip and then it's going to run it Okay, so That's the basic idea and that'll that'll finish when it's all said and done And so this is like a criteria. This is like a search We're looking for lines that match the string that have their string from as their prefix Now when we look at the output of this, it's kind of weird We see Kind of these little blank lines that show up blank blank blank blank blank blank blank blank What's going on here? What's going on? So let's take a quick look The problem is is new lines Well, I mentioned that the file has new lines in them And so when you do the for loop, it doesn't throw the new lines away as you might expect It would be kind of nice if it did but it doesn't it actually shows you when it when you read It reads that first line up to and including the new line and gives you that back as the variable So that is the first new line. So that means it's going to go down And then the print statement actually adds another new line So that's the the second line of the file has a new line at the end of it and the print statement adds another new line So if we take a look at the code There is A new line oops come back If we take a look at the code this variable line has a new line in it Oops, where am I at? I'm in the wrong slide. There we go Yeah This is what I want to do If we look at the code, there's a new line in here and then the print adds another new line So the print adds a separate new line And that's how we get two new lines the print statements new line and the new line from the file Here's how we fix it and you're going to write this code a lot because when you're reading text files You end up with a new line and often you don't want the new line, but thankfully As we saw in the previous chapter where there is a nice little function in Python for strings called strip that allows you to throw away white space and to review Remember white space Is anything that doesn't print and this new line is not i'm non printing characters So our strip gets rid of it So it's a way to get rid of white space and our strip does it from the right end So it's the right end of the of the of the string And so If we just are going to loop through all the lines in the file We say line equals line our strip and then this variable no longer has the new line at the end of it We have our little if statement and if we print it Then this line the data has no thing and then the print the data has a no new line in it So the print only goes down one and so now we have single spaced Output and so you're going to be doing that a lot It's really common to read through a File and then just strip the new line or any trailing space off the end of that Now there's a couple of ways to do a loop like this and let's let's just think of this as We're looking for a line a file with lots of different lines in it And we want to ignore all the lines except some say good lines And we want to do something with those good lines or the lines we're looking for needle in a haystack This is like searching for a needle in the haystack So if you look at this code at a high level, we're going to loop through everything And then we're sort of picking which lines are and these are the good lines down here Now often we have a bunch more code that we want to do and we're not just printing them But we're going to do a lot of code So sometimes you actually structure the loop a little bit differently And so the way to do it and this is going to do the exact same thing It's just a little different way of thinking about this loop So the top part is the same we're stripping it and what we're doing here is it's everything's the same here Except we add this is not if the line does not start with from that's the Translation of that if the line does not start with from Continue so basically we have a skipping pattern So the lines were not interested in we skip so we come down we you know skip a lot of lines And then we find a line that's good and then we fall through So this is the good code and then we have all the other good code that we want to do to that line We have that showing up down here Um And so there's just two patterns that are two ways to do the exact same thing So another way to select the lines that we're interested in is to use the in operator So we talked before about the in operator and how that works So we're basically going to use the continue skipping method So we're going to read all the lines these first few lines if UCT dot ac dot z a is not in the line skip it And so this is going to print out all the lines that have the string uct acs at a in them in them And so you see this is the output of the program dot dot dot dot dot Sometimes you'll have programs that want to read different files often I give assignments where I say show me how this program runs on the short file and then show me again How it runs on the long file just like this and so the way we do that to input the file name It's that are making the file name be a constant to the open call We make the file name be a input So we just run an input statement Which gives us a prompt and then we type mbox dot txt and then that shows up in this variable f name It's a course of string all the time and we pass that into open and then we open it and then we do you know the count operation So if we enter n box dot txt it counts 17 97 Subject lines in m box and if we give it m box short it says there are 27 subject lines in m box And again, this is another one of those ifs and it's just counting but only counting lines that match a particular A particular pattern Okay, so now the user can also type bad file names and we need to be able to deal with that as well And so we we're taking a small small change To the code the danger the dangerous code is this line right here this line right here It's going to trace back if that file doesn't exist. So what do we do? Well, we're going to just expand that the rest of this program is exactly the same The only thing's different is we we've got this line We took out insurance on it And we know that it might blow up and so we we have it in a try and accept block So here's how the code runs So, you know the input runs we type in a good file name. It comes in here This works and so it skips the accept and so it runs the code and prints out the count So that's the good pattern the bad pattern is Here we type in a bad file name. It comes in the try accept This file name is nanabubu and it's going to blow up. So this line blows up So it jumps down into the accept code prints out file cannot be open. So prints this out Now this quit is really important Because if we don't put this quit in here, it's going to continue down here And that's going to blow up here because file handle is not defined properly at this point And so what we have is we have this quit quit is a special function where it comes in And never returns. So this is a way to terminate the entire python program Silently with no trace back, right? So we put in our own error message So we look like we're professional say if we could not open this file And then we stop if you don't it's going to come down here and it's going to trace back Trace back right there.  So The quit is useful when you want to stop executing because you've detected some kind of an error So that's a quick zoom through opening and reading through files and doing some patterns Most of the rest of the programs in this course are going to say open for our strip Do look for and then do something interesting That's going to be our loop that we're going to do over and over and over again And now we see how this looping and if and iteration and variables Are are starting to come together and you can actually sort of do a program that does something useful But before we get to too many more programs We got to switch a little bit switch gears and talk up next about data structures and that is the shape of data Um, and how we can use more intricate and complex variables to help solve our problems Hello, and welcome to chapter eight. We're going to talk about lists in this chapter Up to now we've been talking about algorithms algorithms are the concept and computer science of Using the programming language to express the steps that you want the computer to go through to solve the problem Read some data Convert it to a floating point number check to see if it's greater than 40 do one thing if it's greater than 40 Do another thing if it's not then print out the result or Open a file read everything if the first line starts with something do something if not skip it And then add all the things up Those are steps. Those are a series of steps And hopefully by now you're getting to the point where you have a good understanding of steps But there's a whole other side of computer programming and we call it data structures and data structures Is not the steps, but instead clever ways that you Lay out the data and clever ways that you make sure that the data does what you want it to do And so that's what we're going to start talking about now Lists are the first and most the simplest data structure strings are kind of like data structures But lists are probably our first real data structure that we're going to think about and design and make use of effectively But before we talk about what is a collection We should talk about what is not a collection. So we're familiar with what a variable is We know that a variable is a little piece of memory that's got a label on it And then an assignment statement, you know sticks a 2 into x and then x is and then 2 is in this little Covered and then it goes to the next line and then 4 goes into x and so the 2 goes away and the 4 is there A key thing is you can't have more than one variable at any given moment Right and more than one value in a variable. So when we move to collections Collections are more like suitcases. We can put lots of things in them We have ways of organizing them and as we go through lists and dictionaries and tuples We'll see how there are different ways to organize them and as a matter of fact, we've been talking about lists for a while Every time we use one of these square bracket syntaxes in earlier programs We've been working with lists and so this is technically a three item list with three strings got commas here Joseph has one string glen and sally or another string and here's another one That is another thing and the list is basically It's a list constant and it's being assigned into a variable So this friends variable has three things in it. So that's different than what we've been talking about before So these brackets and bracket structures with square brackets are those lists And so the print is just a print with parentheses to get the print to work But one 24 76 is a three item integer list Red yellow and blue is a three item string list But it doesn't all have to be integers or strings python can handle Different things and different kinds of data in different positions in the list. So red 24 98.6 a three item list with a string an integer and a floating point number And while we're not going to use this too much for now This outer list is a three item list and the second item is another list So this is kind of alluding toward what we'll do when we start talking about data structures And that is we have a structure and then we have another structure inside of it And sometimes this can get quite complex and we're doing this for a reason this here has no reason Just to show you that it's possible that that list can be made up of lots of things Including other lists and of course there is also the notion of the empty list And like I said, I have had to be able to tell you about lists all along. We use them in for loops We can put lots of things here. We can put file handle here We can go through the file we can put a string there We can go through the characters in the string and in the list and the iteration variable then goes through the Successive elements of the list and that's why this prints out for y 4 3 2 1 and then the loop is done and it prints out last off So we've been using them and we've been actually iterating through lists with four statements all along So the for each I mean the four statement You know has has been something we use with lists and every when you just need to go iterate through the list and go Through every item in order The four the four is a great way to do that So friend is our iteration variable friends is our list variable And so that says friend is going to successfully take on the value joseph glenn and sally and print out You know happy new year joseph glenn and sally runs three times once for each of the values and the iteration variable advances now I do want to make it really clear that the choice of friends uh And friend uh singular and plural is arbitrary and capricious. It happens to be convenient and intuitive that the iteration variable is one and the list variable is more than one But python has no idea about singular and plural as a matter of fact python would care It would be totally equivalent for python to do the same thing to have the list variable bz And the iteration variable bx x will take on the successive values of these three things Now am I being nice to you by calling this list friends and this iteration variable friend? I am but I also don't want it to confuse you if you're just a beginning developer So just like strings we can sort of look within list part of the Thing is when you put more than one thing in a data structure you need to get them out And so lists have positions they maintain order And so the first thing in the list is the sub zero position sub one sub two just like strings They're zero based just like european elevators. They're zero based So if we take a look and we say oh friend sub one, that's how I read that the little square brackets When you take a variable here and you say friend sub one remember singular and plural don't matter Friend sub one means glenn because this is the zero and that's the one and then sally's the sub two And so that's what prints glenn out in this particular thing Now lists are mutable mutable is another word for changeable that can be changed Meaning that a list has three things you can change this thing right in the middle if you want To take a look at what's not mutable strings are not mutable So if I take a look at assigning banana into fruit while fruit sub zero is a capital letter b Could we imagine for the moment that we could change fruit sub zero zero to lowercase b? Well, the syntax would be how you would do it if you could do it, but it turns out that Strings are not mutable meaning they're not changeable once you create them And that's why when we do things like lowercase or uppercase We take a look at the fruit and we say give me a lowercase copy of that And then we take the return value from this and we store that in x and that's how x becomes a lowercase banana But fruit is still the original one. So fruit has not changed compare and contrast that with A list though here we have a five item list two fourteen twenty six forty one And we're going to do the sub two position And the sub two is zero one two So that's that one right there and we're going to assign a 28 into it So that 28 is going in here going to wipe that out and put 28 in so we can do item assignment in lists By putting a bracket syntax on the left hand side to say don't just put it in the variable Put it in this position within the variable So that's what that's doing and when you print that out to 28 everything else is unchanged I mean the whole list is there there could be a thousand items in the list and then you're changing the second one We have a function called len we've been using this len function all along To take a look at how long strings are it counts the number of characters in the string So that's a nine character string if we have items in a list Len tells us how many items there are it's not like how many characters there are it's the number of things And each thing doesn't have to be a number it could be a number or a string or even another list And len is the way to say hey how many things are in there There's a function that returns a list of numbers and we use it As we'll see in a second to construct specialized loops to go through lists So let's take a look at this range function just for a minute So range takes as its parameter the number of numbers that you want returned So I want like I'd like a four item list with the numbers zero up to but not including four And so it just turns out that that is really useful for constructing Four loops that are counted for loops that's go to zero to the one to the two as compared to the you know the definite loops that go Each way go through each one And so it's a common thing to say okay We know how many things are in this list there are three friends And if I put combined range and len so I take len friends, which is three And then I take range sub three I get zero one and two And so the interesting thing is this zero corresponds to the first one One corresponds to the second one and two corresponds to the third one Okay, and so we'll use this to construct loops especially when we need to go through a We need to go through an array and remember what position we're at And so here's just an example of two different loops This is a a four loop that's just going to go through whatever's in this list So friend is just going to take on the successive values And so it's going to print out these three things just as you would expect And if you don't need to while you're going through the loop know the position your relative position from the top in the In the loop that's that's okay, but sometimes you want a little more sophisticated loop And instead you want to be able to Loop through where you you know the position And so what we do instead is instead of looping through that list itself We do range len friends, which gives us Zero one two and then i takes on the successive values zero one and then two So this loop is going to run four times and i is zero the first time And we might even just look up the value inside that Subzero value so we get joseph the first time so prints out happy new year joseph goes and i becomes One now and so it gets it gives us glad and that prints out away you go So if you look at these two loops If you look at these two loops they really do the exact same thing The only difference is is this we allowed the four to find its way with the iteration variable through And here we created an our own i variable that went through the positions and they're dense There's no gaps in here. So it's zero through zero through two that it goes through so these two are equivalent There'll be times when you'll want to use one in the other I tend to prefer the first one because it's prettier as long as Um as long as it works for me So that gets us started with loops will be back in just a bit Okay, so we've taken a look at loops and now we're going to just take a little A bit of a look at some of the operations that you can do with loops Um 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 clean floating point numbers integer number strings Etc. And so the plus uh similarly works this way with uh lists the plus looks to its left and looks to Right and says what am I adding and in the case that i'm adding the list one two three in the list four five six It concatenates them together and this way it sort of functions like a string And so we get one two three four five six It's just concatenate list this list to another list and it doesn't change a or b just like in any kind of assignment statement Uh calculations on the right side don't change the variables and they produce a new variable and then assign that into c You can also also use list slicing and it's it's easy to remember if you remember how strings work list works exactly the same way So it's you know, of course, it's a little tricky. The first number is the starting position. They started zero So one is right there. So it's the zero position the one position start at one Right, but go up to but not including three. There's One two three so this goes up to but not including three and that's why we get forty one twelve out of that So up to but not including i'll just say that over and over and over again If we do uh, 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 four So that starts at the beginning goes up to but not including four and so that's how we get that piece right there we can say, um Start at the position three zero one two three start at position three and go to the end Now the fact that the number three is in here is sort of irrelevant Three to the end is those three numbers and then you can do the whole list with slicing as well Again, these pretty much are 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 in list I often just use the dur command to remind myself of them a pen will look at count Looks for certain values in the list extend adds 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 pop pulls things off the top remove removes An item in the middle reverse flips the order of them and sort puts them sorted order based on Based on the values So let's look at a couple of these Um 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 um Say list parentheses says make me an empty list and then assign that list into stuff So stuff is now a 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 dot 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 We got book at 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 um and the cookie We have an in operator Uh works pretty much like the in operator in a string Uh 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 Is 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 is 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 in for strings Uh same kind of stuff Lists are in order and they're sortable and so this is something that we take good advantage of Um a lot of what 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 friend of inside of list That's just the sort method. So here we you know put three values in zero one two positions zero one and two joseph glennon sally And then we tell the list to sort itself and then we print it out Now this is actually sort of the list in place, which is different like than 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 and lowercase in a very consistent pattern But the list has changed and if I look at list sub one that is the second item Which is joseph that prints out right down there There's a whole bunch of built-in functions to help manipulate list The other things I was showing was uh method sort is a method that's part of list But there are other functions that take list as their arguments Um, we already talked about the lend 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'd have known this a 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 they're kind of easy loops to understand um, and so uh, those are there and 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 uh 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 Uh 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 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 Okay, and so that 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 and 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 divide it 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 Uses more memory, but Uh, it doesn't really matter by the time it's all said and done and so for you This the the difference between these things is not all that significant But it's important to understand that they're just two techniques to accomplish the same thing with lists So now we're going to wrap up and talk a little bit about how strings and lists are related They're sort of related in that they both have zero base things and we use the square bracket operator To do various things But there's a lot of situations where we're looking at our data and we're combining the use of lists and strings So let me show you the first thing probably the coolest thing We're going to use it a lot the rest of the class and that is the split function So let's take a string. We've got abc here. It's with three words What we're interested in the fact is that there are spaces in this word And what split does it says, you know, I'm going to look through this thing I'm going to find this and I'm going to break this into pieces And I'm going to return you a list of the separate individual pieces So read look for blanks and break it in pieces and give me back the pieces So I'll print these out and now you see that it's a list With three items with three words the spaces are gone, but it's given it to us So it's like split this into words, please and give me the individual words and give me a list of individual words Rather than a big long string with spaces in the middle of it And that is a quick way to go from a line and and It's really common a lot of things we're going like go get the second thing or the third thing or whatever So the splits really nice because then you can just grab stuff And so you say oh, how many things did I get? Well, I got three the len function tells us that and I can print the first word I got Which is and with the sub zero and that'll be like with will be the first word because that's the sub zero position So I read something I split it I can say there's three things and I can look at stuff the first word basically without really knowing much now if you remember Earlier and we'll we'll see this we used find and Slicing to do a similar kind of thing, but people tend to prefer the split And you can you can you know oops go back You can also then Loop through them So you can split these things into stuff as a word and then go through the with w and then it's going to and it's going to go through W is going to take the success of with three words And so you can make a loop by reading some data splitting it and writing a for loop And then it's effectively going through the words in that line of data And so that's a really powerful concept that we'll use in a lot of the programs that we're going to write Just a couple of bits about this and how it works Split with no parameters here. It looks for spaces, but it also treats a bunch of spaces as a single space And so it's pretty smart about that and so even though this has a lot of spaces between lot and of You only see a lot of all the spaces are gone. It does something special about spaces It's really white space so tabs or new lines or other characters would also qualify In split basically Now you don't always have to split based on spaces and a lot of data that you're going to run into You're going to want to split on something else And so here's some data that looks like we're using colons to to separate the first second and third piece Now if you just call split splits looking for spaces And so split gives you back a list of the things broken apart with spaces But there's not a single space in that line and so we get a list see it's a list But there's only one item and the semicolons are sitting there split doesn't go like Whoa, this looks like it should be semicolons, you know splits job is to use spaces and split the string based on spaces okay, but Given that this is something we like to do you can tell split what character you'd actually like to split on Now it's not quite as clever when splitting on something other than spaces It doesn't understand that you know, if there's a bunch of semicolons in a row It still thinks of those as splitting points to split But in this particular case where there's no spaces that you know, and it's going to split that So it says split this based on the semicolon based instead of being based on the um the space And so if that you take a look at what comes out of this We split on semicolon now we have a three item list And we get first second and third and a lot of your data comes out of some logging system or some router status updates Who knows what you're looking at but the delimiter is often Something other than space and you can do that with split So this is a useful thing when parsing things like our email address, right? We wanted to get things like the email address this second piece off of the line And so we can use split to take advantage of this and so here's a little loop That's just going to print out not the email addresses, but instead The day of the week we're going to print the day of the week out for all these things. How do we do that? Well, we can observe really quickly that If we split based on spaces We it's the 012 it's the two positions so we can quickly write a bit of code that You know opens the file Then loops through the lines. We do this all the time now A strip takes off the end of the new lines We can check to see if it starts with from space, right from space is our key So we're ignoring We're ignoring all of the lines that don't start with from sprays But then we find the line that starts with from space and we split it and then we just print out the second word And so we get the second word of the lines that start with from and that's so how this thing works now Sometimes we want to dig into deeper and we will take something split it and then Split another piece of it again with a different delimiter So let's just say that the thing that we want to achieve is getting the part after the at sign for email addresses And we did this with again find and pose and stuff like that, but you can use split to do this as well So the first thing we're going to do is we're going to take this line. We're going to split it based on spaces, right? chop chop chop Chop and the fact that there's an extra space there doesn't matter split happily just like zooms through that And then words sub 1 0 1 2 word sub 1 is this email address So we'll put that in a variable called email and so email will be a string That's just this so in two lines We've pulled out this second address into a variable Then what we're going to do is we're going to re-split that We're going to take this string We've got and split it based on at sign because we know it's an email address So we get a new set of pieces the first part is the person's name and the second part is The host name that their email is hosted up And then what we can do then is we just happen to know that We just happen to know that these this is the zero item and this is the one item so we can get at that So the interesting thing of going here if you think back to how we did this before with find and pose and all that stuff It's really a lot cleaner and we don't For me I can I can look at this after you understand it and it's easy for me to understand that it's correct Whereas that pose stuff you got to add one and start the second find after just just remember that And this is a lot cleaner way and this is a more typical way of pulling this kind of information out of a line So in this chapter we've talked about lists we've talked about the concept of collections That's our first data structure. We're not just doing algorithms that we kind of know algorithms now But now we're going to do data structures and the next this chapter and the next two chapters are our foundational data structures And then we'll like everything we'll make more complex data structures by composing those data structures Together we've looked at how strings and lists connected together and how split works and these are all really powerful tools That we're going to use going forward Now we're going to take a look at how we would write some code to do some parsing Read some data As a matter of fact, we're going to read through our famous mailbox data Look for lines that begin with from from space And extract the third word. As a matter of fact, we already have some of this code already written We're going to debug it. We're going to look at code and we're going to debug it. So here we go It here we have it and it's a it's a pretty basic Program it opens a file loops through the file throws away the white space Splits it into words and checks to see if the 0th word the first word is from and if it's not we skip and read the next line And otherwise if we find a line that starts with from space, then we print the third word, which is word sub 2 Okay, so this is what we've got and we carefully saved this file Into the same folder that we've got uh ex 0 8 And so let's go ahead cd desktop python for everybody ex underscore 0 8 and so this is Some files we got our day of the week python and our inbox short. So that's sitting there. Okay And so let's run this program. This is the program we've got right here python 3 d o w dot py and It doesn't work now by now you've seen a few trace backs and there you go So, you know the When you look at a trace back you think to yourself well, I made a mistake And you've gotten pretty good at looking at that line. So there you are you're like this is the line there must be something wrong on this line And you want to change it But but that line is not actually the problem in this particular thing And so you got to be careful sometimes And one of the things that you didn't notice in this one right away is that it actually worked It printed the first line out. So if we take a look at our dataset It found the line started with from space it split it and printed out the third word And it blew up later And so part of the problem is that we don't know what it was doing when it blew up And so the first thing I'd like to do in this kind of a situation is Um find the line and make sure there's a print statement right before it And so I'm print Words colon and then comma wds I want to I want to print right before the line that blows up so that I know I know really when this finally does blow up what was going on in that line So i'm going to run it again Do I forget to save it? No, I forgot to save it. Look at that. See the little blue dot forgot to save it So now we see a whole bunch of output and we see that it's actually doing a whole lot of work before it's blowing up And so you see that it it prints the words out from that first line and prints out saturday Which is exactly what we expect It's the third word in the line and then reads a whole bunch of stuff and it's actually what's doing now is ignoring Let me just put something here I'm gonna say print Ignore So I can keep track of when these lines are being ignored. So let's run it again and have the word ignore pop up Right and so it's doing a lot of ignoring It it prints finds these words prints out saturday Reads this line and ignores it reads this line and ignores it reads this line ignores it So a lot of stuff's going on here that you might not realize And so We have to take a look at what the problem is and so the it is now blowing up words sub zero And now we can scroll down and we can look at exactly what happened right before the trace back So we really now know Exactly what happened before the trace back and the interesting thing is Is that there is an empty Empty string. I mean empty uh empty array. There's an array with zero items. So I'm going to print the line out to print Line colon now. I haven't changed my program at all. I'm just trying to figure out what's going on here So I'll save that and I'm going to run it And we've got a lot of stuff and and it's still working it reads a line It reads a line splits it into words and then prints out saturday, which is the third word on the line Now here it reads a line and this line is a blank line And it has because it's a blank line the split retains returns no words And that's what blows up and the problem now is oh wait a sec list index out of range So word sub zero is not valid, which is the first word when there are no words so This is a statement that works most of the time now. You might think oh, I want to just put a try and accept in there well The right thing to do is to say to yourself. Oh, wait a second if The I don't have enough words if the length of the words is less than one Continue So basically it's going to come through here. It's going to split it And if we don't have any words meaning it's a blank line, then we're going to skip it. So let's run that So now this ran all the way to the end. It did a lot of stuff and it did not blow up specifically didn't have a Trace back another way to protect this would be to We'll take this part out. This is called a guardian pattern right guardian pattern because this This is dangerous This could blow up But this it will it won't blow up if it makes it past here and it won't come through there Under the conditions that are causing it to blow up another way to do this might be to protect it as follows to say oh, wait a sec If the line is a blank line No Continue So now what we're going to do is we're going to skip blank lines. I even say this Print skip Blank So if it's a blank we're going to skip blank and keep going This will skip blank lines. It'll come through here And this will skip lines that don't have from but because we're not processing blank lines word sub zero always works So I can run this code And it works again. So here we have a blank line. We skipped it here. We have a blank line. We skipped it Now here we had a non blank lines. We parsed it, but then we ignored it And then up here we'll find a from somewhere Let's find it from It comes No, there's ignore ignore I got too much debug print. I can't find it here. I'll just hunt for from with find Okay, so there we go There it's from and we print the thing out. So we're getting a lot of Uh, a lot of extra stuff. So I'm gonna I'm gonna comment out some of these uh, Debugs And I'm actually just going to get rid of this whole skipping of the blank line I'm gonna do with the words. I'm going to go back to the the guardian we had before if The number of words that we got len of words is less than one continue Okay, so now this is going to be a working program Oops, I gotta take another print statement out Got to take another print statement out. We sort of know what we're doing here Okay So this looks like a pretty safe thing this guardian is protecting this dangerous I get rid of that one too This is the words that could was our trace back And nothing else in this thing changed from when we started except we've added this little guardian Now the interesting thing is if it comes through here and prints words of two What happens if somehow we find a line that has from is its first word And there's only one word on this is going to blow up so we can make our guardian a little stronger And we can say, you know what? We're gonna we're going to skip this line if it doesn't have three words in it So it has to have at least three words And if we see less than three words, we're going to skip it and that just makes the guardian a bit stronger And so the program works safely and and you see these things where you sometimes you want to check to see Reasonable that your assumptions about the data are reasonable and skip things where the data is not reasonable Now there's so that's one guardian pattern Let me show you a slightly different way to do this and this is with an or statement So i'm going to take this code Copy that and put it here with or Get rid of all this stuff This is the guardian In a compound statement so Or saying is if there are less than three words On the line or if the first word is not from Continue now we're doing this in order Because the way it works is or is true if either that's true or this is true But if it knows that this is true, then it doesn't bother checking this and the checking of this is what blows up What causes the trace back? So if we flip this order it would fail if we do to do it in this order it will work So let's let's do this one right That works But if I if I get this backwards It's going to check this before it checks this And we're going to go back to Failing again So you got to get the order of these things right you the guardian Comes before in the or The guardian comes before and if this is true, then it doesn't check this This is called short circuit evaluation where it knows that as long as this part is true It doesn't evaluate this second part And so now we have a guardian in the compound statement Uh, you will you'll see this a lot Sometimes if it's more complex you do it in multiple statements or you sorry fall through check for sanity check for sanity And uh only run the code So I hope that that was uh useful to you Looking a little bit about how to debug where you don't just start Chopping on the line that had the problem. It's not always that line because we never did change that line Although we did change it a little bit at the end. We added this guardian here, but we also fixed it without it Um, you know sometimes you add some print statements to figure out what's going on before you just start chopping on that line Um, so again, I hope this helps. Thanks Hello and welcome to chapter nine now. We're going to talk about python dictionaries. Python dictionaries are Probably the thing that most programmers love the most about python Because they're very powerful. They're like a little in-memory database It's the second of our kinds of collections and probably the best collection To review what a collection is it is a situation where we are going to have a variable like a list or a dictionary That we can put multiple pieces of information in rather than a single piece of information And of course prior to collections, we would put something into x and then we would put something else into x and it would be over written And uh now with lists we can append things on to the end and so if we compare lists and Dictionaries the list is sort of the organized version of the collections It everything stays in order you add something it always adds to the end You take something it sort of compacts itself It's zero through the n minus one where n is the number of items And so it's very organized kind of like a Pringles where the potato chips are nicely stacked Dictionaries are messier You can put things into dictionaries. There's no real sense of order in dictionaries Everything has a key So you sort of throw things in and they kind of mix around in there somehow and you pull things out based on the key It's like you you sort of stick a label on it Um, you know where you say, okay, I'm going to take this thing And I'm going to put chuck on it and I'm going to take these sunglasses With the chuck label and I'm going to throw it into the dictionary and I'm like, hey, give me back chuck I'm like, oh, here's your sunglasses because you mark everything This is like the key This is the value I took a pair of sunglasses and I threw it in so it's kind of like a purse or it's a sort of like a mess And so the idea is is you have these labels that you put on everything that you're going to throw in Like I'm going to put so if I won't stick to my keys You know what else I got here I'm going to stick a label on my pen A chuck label and I'm going to store a pen in my dictionary with the chuck label And so it's like having a purse or a bag or a backpack where you have things labeled and you can You can throw things in and label them and you can shout into your bag and say give me the calculator or give me the candy Or whatever that is that you have labeled them You have to come up with the labels and then you can use the labels to get things back out And like I said, they're probably the most powerful thing and and they're basically this concept that's generally referred to as Associative arrays, which means they're like lists, but they have these keys And so the associative means the association between a key and a value Whereas in a list there's a position in a value the position is less powerful and less flexible Most modern programming languages have this notion of associative arrays if they don't they're sort of unpopular because Once you get using them or like, whoa, they're so powerful If you ever find yourself in a language that doesn't have them You'll you'll freak out they get have different names like property maps or hosh maps or property bags Depending on the language you're using but they all are the same thing. They're key value pairs So the idea of a dictionary is that or the idea of any collection is putting more than one thing in And then the difference is is that you have ways of of Indexing it so this basically line says let's make ourselves a dictionary just like we constructed an empty list And I want to store 12 into this dictionary and I want to label it money And so on the left hand side when we use this money, that's the label that we're going to give it And so 12 is being placed in the dictionary. That's like taking the 12 throwing it in the dictionary with a label of money. I can't yeah Three's going in with a label of candy and 75 is going in with tissues And we say what's in there and there's no order to it and sometimes the order can even change inside of a dictionary Although there are more advanced versions of dictionaries that maintain some kind of order. But for now, let's just not worry about the ordering of them If we say what's in there you say, oh, there's three things in there. There is 12 75 and three And stored under the keys money tissues and candy respectively. We can ask Using the index operator. What is per sub candy? That's like saying hey, give me back candy and outcomes Um, uh, the number three which is that we can update stuff So we can say like go grab the candy version add two to it Make five and then store that back into candy. And so now we see that candy has been up to set up to be five and um and so If you look at the difference between lists and dictionaries, they both can have new items added to them We haven't talked a lot about deleting but items can be deleted from them The difference is is the indexing mechanism how we look things up how we store things And how we look things up. So we make an empty list. We make an empty dictionary We add 21 to the end and we add 183 to the end and we ask it and says Oh position zero is 21 position one is 180 183 do we don't see the positions when we print it out because it's sort of implicit Here we're going to in mark 21 with age and stick it in and mark 182 with course and stick it in And then we're going to print it out and there we got course and age mapped and we can add 23 and stick it back in age and that overwrites. So the 21 becomes the 23 We can do the same thing in a list except we say lists of zero because in lists the indexing Is position and so this 21 becomes 23 And again, you just look at them and you can think of each of these as Pretty much doing roughly the same thing except the indexing mechanism The values are the same but the keys are different. So in lists the keys are always the position And you don't get to assign those other than the fact that the order in which you put them in implicitly assigns a position and in dictionaries the the key is a string You can actually use other things. I use strings a lot in this lecture But that's just to kind of keep things simple until you get good at it You can actually use numbers as the dictionary index The dictionary keys if you want, but the values are things you put in and manage in those dictionaries So we can just like lists. We have dictionary literals And what's nice about dictionary literals is that they use the exact same stints acts as the print out And so it starts with the curly brace ends with the curly brace And then has a series of key colon value key colon value key colon value And this is sort of the associative array bit. We are associating one with the key chuck We are associating 42 with key fred or associating Chan and 100 then we printed out it kind of looks exactly the same And so the print statements in python are are nice in that you ask What's in a thing you show the stuff and it shows you in the syntax that if you type that into python that would be how you do a constant And if you just say Empty array you see you see me also do di ct This is a constructor where you say make a new empty dictionary. This is an empty dictionary constant These two things are pretty much the exact same thing. This is a shortcut to doing this The the the empty curly braces is a shortcut to do the Construction so up next we're going to talk about sort of one of the really common applications of dictionaries and that is Counting so now we're going to talk to you about one of the common applications of dictionaries and that is making histograms It's counting the frequency of things And so if you think of a