Download and watch the episode here:
Coding 101 14
Shannon Morse: Today on Coding 101 we have For loops, if then statements and Mod.
Netcasts you love. From people you trust. This is Twit!
Bandwidth for Coding 101 is provided by Cachefly. At C-A-C-H-E-F-L-Y dot com.
Fr. Robert Ballecer: This episode of Coding 101 is brought to you by Squarespace, the all in one platform that makes it fast and easy to create your own professional website or online portfolio. For a free two week trial and ten percent off, go to squarespace.com and use the offer code CODING.
Fr. Robert: Welcome to Coding 101, it’s the Twit show where we introduce you to the wonderful world of the programmer. I'm father Robert Ballecer.
Shannon: And I am Shannon Morse, and for the next thirty minutes we are going to get you all coded up on everything you need to know on Python to become a code warrior.
Fr. Robert: That’s right, you know Shannon last week we talked a little bit about loops. Now we know from the C# module that loops are an absolutely necessary part of programming.
Shannon: Absolutely, they save you a lot and a lot of time.
Fr. Robert: Yeah, yeah.
Shannon: So I decided to go ahead and do a very, very simple loop from my example this week and it basically is what you taught us—
Fr. Robert: Yeah.
Shannon: …except a little bit more complicated, just a bit. So I'm going to pull up my loop real quick. Shannonloopexample14 and I’ll show you what output looks like.
Fr. Robert: Boom!
Shannon: So it simply says—
Fr. Robert: Whoa okay, that’s lot of numbers.
Shannon: …up at the top, let’s do some math. And you'll notice over here it increases from twelve to fourteen, sixteen, eighteen all the way down until the bottom it gets too, whoops way to far, it gets to one hundred. Press enter to exit.
Fr. Robert: So you did an increment by two instead of the standard increment by one.
Shannon: Increment by two, exactly. And I started my number at ten and I also added the print at the beginning that just said let’s do some math. Now we also figured out that if you're doing this in IDLE on Windows and you have a While number and then whatever and then it ends with a colon, it will automatically do that indent for you so you don’t need to tab over anymore because it’ll just save like that. So it gives you a little bit of extra functionality whenever you're using IDLE. And I like that, I didn’t know about that before I did this loop. So it’s very simple, very easy to use and it’s a good example.
Fr. Robert: And something else about this, I think it’s important to point out is so you did the while loop and remember in Python, whitespace counts.
Fr. Robert: It’s not like C# where you could actually crowd everything together as long as you end each line properly, it’s all good. In Python it’s important where you put the lines and if you look at Shannon’s code she indented the code that went under Whileloop..
Fr. Robert: Now at the end of that condition, remember the condition is determining whether or not the loop is going to run, in this case it’s number less than a hundred, she puts that colon there and then everything that is indented after the Whileloop will be part of that While code. In this case, while she’s adding another line of code, that second while—
Shannon: It automatically indents for you.
Fr. Robert: Exactly yeah, it automatically indents and once you bring a line back to the start, it’s no longer part of that code block that gets looped.
Shannon: Right, so very simple to do again and I really enjoyed doing that. So I'm going to go ahead and close that. We also had a couple of user samples that I enjoyed checking out this week.
Fr. Robert: I love user examples.
Shannon: They’re a little bit more advanced than what we have been showing so far so there’s a couple of things in here that you might not understand yet but we will get to those. The first one is a simple tic-tact-toe game and I’ll go ahead and open this so you can see what it looks like when you play it. So it says welcome to tic tac toe, player one’s name, Shannon. Player two is going to be padre.
Fr. Robert: Oh, I hate tic tac toe.
Shannon: All right so my move Shannon, okay. I'm going to choose nine.
Fr. Robert: Six.
Shannon: Six. Okay so we have one x one zero there. My move, I'm going to do, I’ll do one.
Fr. Robert: Three.
Shannon: Three. I think I'm going to win. Let’s see, seven.
Fr. Robert: Well no I'm not winning this time, four. What? What? I'm going to force you to win.
Shannon: I'm going to do x or eight. Yay I won. All right so you know, very simple to use. Now if we look at his code.
Fr. Robert: This is actually a nice piece of code. So we’ve got input, we’ve got graphical output and we also have state checking. Because it has to check to see if one of the players has actually won.
Shannon: Yes, exactly so here we see that he’s printed the tic tac toe boardgame right here and he named each one something different. Board nine, board six, board three so he has each one named differently.
Fr. Robert: In an index.
Shannon: Yes, exactly.
Fr. Robert: Right, right nice.
Shannon: So a very easy way to tell each space apart. And then if we go down he has a bunch of if statements. If board two equals test and board five equal, equal test.
Fr. Robert: Right and it – so in this case what he’s doing is he’s checking all the possible ways to win and he’s seeing if all of those states are the same. If they’re all the same, either that zero or the x then he knows that one of the players has won.
Shannon: And then if we scroll down you'll see – so this is where the game starts so depending on whether one is a zero, one is an x it’ll determine who is winning and who is losing. And then it will trade back and forth between the two players as you continue to go down.
Fr. Robert: This is actually a brilliant piece of code because it show you again taking something as simple as tic tac toe and as we all learned war games, it’s the game that going to teach the computer how to sentient.
Fr. Robert: But it allows us to really look at what you need to do to break down logic into something the computer can understand. I say awesome, fantastic job.
Shannon: And check these out, we got a Whileloop. Ha awesome.
Fr. Robert: Woohoo.
Shannon: I love that he included the Whileloop in there. So here we have the player returns and then who wins the game and then if it’s true or not.
Fr. Robert: Yeah.
Shannon: So it’s very – it’s kind of complicated in a way but when you read through the code and you take your time to actually look at it, it make complete sense. Very, very interesting and I like that one a lot. And this one came from, if I remember correctly, that was from Scott.
Fr. Robert: Scott. Well done Scott.
Shannon: Now we also had one from Jerry that I wanted to include as well and I’ll pull up this example. So this one just asks you for a number. So if I give it a number I’ll say – let’s do sixteen. So I’ll hit sixteen and then enter, the number you entered is sixteen. Factor as one, factor two, four, eight. So the list of factors are one, two, four and eight. Interesting!
Fr. Robert: I like that one and again it’s one of these programs that—
Shannon: And you play it again.
Fr. Robert: …it asks for some very simple input and then it gives you a calculated output. I like that.
Fr. Robert: This is really what computers were made to do. To do really quickly the calculations that could take us a little time to do in our heads.
Shannon: So this one’s doing math, yay. So it’s basically doing determining factors of a certain number so you can type in any number that you want and it’ll give you the different factors of that number so it gives you a factor test number and he includes all these really, really important notes in here as well so you understand everything he’s doing as well, very interesting. He has some if and then or if else statements in here and he also has a forloop, pretty cool. And his is a lot more short than the other one.
Fr. Robert: Right, right.
Shannon: But also, again he included those notes in those comments so it keeps it very understandable, it’s very easy to understand what he’s doing in here. I’ll go ahead and close that one so those are my examples this week.
Fr. Robert: Thank you so very much Jerry.
Shannon: Of course if you guys want to share your examples you can always do that in our Google Plus community. You can search for twitcoding101 or go to gplus.to/twitcoding101 to do that.
Fr. Robert: And folks let me stress that, you know if you want to submit an example to us it doesn’t have to be super, super complicated. Obviously Scott and Jerry have some Python programming skills, obviously they’ve got skills as programmers but if you just want to show us a thing that you did, just your first time out that's got as much chance as being on the show as something like a tic tac toe or a factoring program.
Shannon: Oh definitely, I love it when people share their first code ever. You know that makes me excited because you know I'm very new to a lot of these languages as well and I’ve been sharing my beginner code, of course a lot of these are very advanced and they're cool but I love seeing what you guys are beginning to do so please do share them.
Fr. Robert: Yeah, yeah and we want to stress that this is a learning environment—
Shannon: It is.
Fr. Robert: …it’s Coding 101, this is not Advanced super advanced coding for AP students. This is designed to get the beginner in there to really give you an opportunity to jump into the community, post something up and let people suggest things to you. That's one of the things I love about are G Plus community, which is we’ve got people in there like Joe who will say “Hey that’s nice, have you tried doing this?”
Fr. Robert: “Or have you tried adding that?” It’s part of the learning experience of just hacking around with code.
Shannon: Now, I belive that you have some information about websites.
Fr. Robert: Yeah so you know Shannon, I was just thinking I want to create a website, right?
Fr. Robert: But I'm just so bummed out because I know that if I wanted to create a website I'm going to have to worry about the backend.
Shannon: You have to know code.
Fr. Robert: I have to know code. I'm probably going to have to hire someone to do my registry, I'm probably going to have hire someone else to do the hosting. And maybe even a third person who can figure out all the database stuff so that my Wordpress blog works well. You know I want to do it but it gets complicated.
Shannon: It gets so complicated.
Fr. Robert: If only there was a way that I could bypass all that stuff?
Shannon: Hmm, I wonder if there’s a way.
Fr. Robert: You think.
Shannon: I think there might be.
Fr. Robert: What’s that thing on this, is it Squarespace?
Shannon: Oh my gosh, I do, I'm so excited about this.
Fr. Robert: Okay, fantastic. All right, so last week we talked all about whileloops right?
Fr. Robert: And you demoed them and so you brought them back.
Shannon: Yeah, very simple.
Fr. Robert: Very simple right and more than that it’s really the same as what we saw with C#. Whileloops—
Shannon: Yeah it is.
Fr. Robert: They almost work exactly the same right? You have a counter, you have a condition that says whether or not you run the loop and you have a block of code.
Shannon: Pretty much the only difference is how you write the command.
Fr. Robert: Exactly, how you write the command and the syntax, you know. Again it’s that whitspace versus ending each line with a semicolon.
Fr. Robert: Well, we’re getting to for loops and although they work pretty much the same way as for loops in C#, they are a tiny, tiny tad different.
Fr. Robert: In the way that you create the loop at the beginning.
Shannon: Oh okay.
Fr. Robert: So when we did for loops in C# we always had to make sure that we set the beginning value for the variable that does the condition right?
Shannon: Right yeah.
Fr. Robert: Whether or not the for loop runs. And then we also had to make sure that we incremented.
Shannon: Right, that’s true.
Fr. Robert: Because if you don’t increment or decrement the counter then the loop will either always run or always not run.
Shannon: That’s correct, yeah I remember that.
Fr. Robert: And so we always had to make sure we built that into the code. Well the same thing holds true for for loops in Python but the way that you increment or decrement are slightly different. It makes you change the way you think about for loops. Now let me show you exactly what we’re talking about here, let me pull this up. This is what the code looks like for a for loop in Python. Now you'll notice this is about as simple as I can make this. Now for counter, counter is the variable so again it’s the expression right?
Fr. Robert: Is it true or false. But in this case it’s going to say for counter and then we have a function in range and it passes to parameters.
Shannon: Okay so for this loop named counter—
Fr. Robert: Well for the – yeah, for the loop with the counter called counter—
Shannon: Um-hmm, right.
Fr. Robert: …range zero comma three and what’s that going to say is it says for this range starting at zero ending at three, print counter.
Shannon: Ah okay.
Fr. Robert: That’s what that means. Now what’s unique about this is we predefine the range that the for loop is going to run. It’s all right here, we know exactly how many times this is going to run because we can spell it out.
Fr. Robert: Now you got to remember that a for loop in Python is going to be a pre-check but a post increment decrement. Which means it’ going to check it in the beginning. It’s going to say “Is counter in range of zero to three?”
Shannon: And if it is, it prints it.
Fr. Robert: Yes and at the end, it will go ahead and add to the range. So it’s going to start it at zero and it’s going to say “Is zero in range of zero to three?” Yes, go ahead and run that. It’s going to print zero. Then it’s going to say “Well we increment that from zero to one” now it’s going to ask “Is counter one in range of zero to three?” Well yes it is.
Shannon: Yes so it prints it.
Fr. Robert: Exactly.
Shannon: So it automatically knows that you're going to increment by one for this range.
Fr. Robert: Right, exactly so it just kept incrementing and incrementing until we get to three. It’s going to get to three after it prints two and then it’s going to say “Is three in range of zero to three?” and it will say no.
Fr. Robert: Three is not in the range. Three is the upper limit of the range.
Shannon: The end of the range.
Fr. Robert: Right so if I'm right here when we run this, what is should do is it should show us output of zero, one and two. And there we go.
Shannon: And it does.
Fr. Robert: That’s what it does. So that’s how the for loop runs. Again, it’s not super complicated but Python, it demands you think a little differently about how the for loops run.
Shannon: It’s kind of weird that it still prints the zero at the beginning and not the three at the end.
Fr. Robert: Yeah, and again you know these are those things that they change from language to language.
Fr. Robert: This is just how Python happens to handle a for loop.
Shannon: So why would you use a range?
Fr. Robert: Well if you use while, we love using while for menu right?
Fr. Robert: …because we can make it indeterminate. We say keep running this until they want to exit out.
Fr. Robert: You’d really want to use – in this instance for Python, you’d want to use a for loop when you have a determined range. When you know exactly how many times you want to run this thing. For example, let’s say we have an array and we want to fill the array with values. We know exactly how many values are going to be so we can have a range for all those values.
Shannon: Yeah, okay yeah that makes sense, cool. That’s a good example.
Fr. Robert: Versus a while loop which could run forever.
Fr. Robert: Yeah.
Fr. Robert: All right so that’s what we got. Now we’re not just going to do for loops. For loops are great and they're a necessary part – but we also—
Shannon: We’re doing more?
Fr. Robert: We’re doing more. M-O-A-R.
Shannon: It’s going to be a complicated show.
Fr. Robert: We got to talk about if else statements.
Shannon: Oh boy.
Fr. Robert: Now we got a little bit of that last week.
Shannon: I was looking forward to this.
Fr. Robert: Yeah, yeah now this – when we talk about modeling the real world inside of computer code, if else statements are a huge part of it right?
Shannon: Yeah that’s true.
Fr. Robert: Because an if else statements allows us to convert things into that binary thinking of a computer.
Fr. Robert: Zero or one, is it true or is it false.
Fr. Robert: And quite simply and if statement will just look at and expression, it will say “Is this true?” if it’s true, run this code. If it’s not, skip.
Shannon: If it’s not true, don’t run the code, skip it.
Fr. Robert: Right, right so this is what an if statement looks like in its very simplest form if you ahead and go to my computer. I’ve two inputs here, I've got raw input a I've got raw input b. Now the first one says “Please enter a whole number” and the second one says “Please enter a second whole number”. I’ll see that up on my screen. Now it’s going to do a simple comparison, is a less than b?
Shannon: And if it is, it prints out a is less than b.
Fr. Robert: Right so if I run this, I'm going to say “Okay, my first number is two, my second number is four”. Oh, yes two is less than four, which is all true right.
Shannon: So run it again, I want to see what it does if you don’t.
Fr. Robert: Yeah exactly so let’s do this, let’s go ahead and jump back and run it again. And this time we’re going to say four and two, we reverse it right?
Shannon: Right. Nothing.
Fr. Robert: Nothing.
Shannon: Okay cool.
Fr. Robert: So we got to that if statement, we got to the part where it was doing the relation, the expression—
Fr. Robert: …right here and it said “Oh, no, no, no, no, no four is not less than two” so it didn’t run this but—
Shannon: So it doesn’t compile any kind of output because you haven't told it to.
Fr. Robert: Right, so I could put a bunch of if statements but there’s actually something built into the if else statement that doesn’t require me to do that so I’ve got this, I can put if, I can put elif and then else. This gives me three different options right? So I could say if a is less than b or else if a is equal to b.
Shannon: Oh that’s what that is. I was like was that a misspelling?
Fr. Robert: No, that’s else if or else. Now I could also – if I just had two options I could leave out the elif, I could just say “If a is equal to b then print this, else which means any other condition, print that” but in this case I want to have a third option right. I want to know if a is less than b, I want to know if a is equal to b and I want to know if b is less than a.
Fr. Robert: Right.
Shannon: Yeah that makes sense.
Fr. Robert: Okay so now if I run this.
Shannon: Else if, elif.
Fr. Robert: Elif, now if I enter a whole number let’s do the two, four, two is less than four. Let’s go ahead and run it again and switch that around, four and two, you know it’s actually – so now I'm doing that last bit right?
Shannon: So it switches the number.
Fr. Robert: Right and then let’s do the one where we actually have the same output, the same numbers. So let’s say five and five, five is equal to five.
Shannon: Oh cool.
Fr. Robert: And again you know this is all about a decision tree. We want to make a decision tree that runs through the problem that we’re trying to figure out which is is the first number – what is the first number in relation to the second number.
Shannon: Right, okay. So you’re always checking it against the second variable?
Fr. Robert: Yeah, yeah.
Fr. Robert: Cool? Does that make sense?
Shannon: It does make sense.
Fr. Robert: Now there’s a few things in there that again we brought it from C#. If you want to know about relational expressions, just go over to C# because they're the same thing. We have greater than, we have less than, we have greater than equal to, less than equal to.
Shannon: Okay so we can still carry over all the same ones.
Fr. Robert: We can still carry all those.
Fr. Robert: Not equal to, or equal – actually equal to is wrong but the double equals, that’s a relational expression. All right, so that’s our if else statements, right?
Fr. Robert: There is one other thing that I really want us to cover and that’s mod. This is a tiny, a tiny teeny tiny thing that we want to cover.
Fr. Robert: Mods. Modulo, the idea is we do a division. So let’s say I do two divided by two.
Fr. Robert: Okay.
Fr. Robert: Oh no, actually let’s say ten, ten divided by two right?
Fr. Robert: I get five, what’s left over?
Fr. Robert: Well no, I mean so what’s the remainder of the ten divided by two is actually zero right because it goes in perfectly right? Now if I did eleven divided by two, I get five but then I also get one left over.
Shannon: You have one left.
Fr. Robert: Exactly so and if we think about it, if I were trying to come up with a program that would determine whether or not a number was odd or even, I could use that rty? Because if I mod something, if I modded division by two, there’s only two possibilities. Either there will be nothing left over, which means that it’s even or there be—
Shannon: There will be something left over which means that it’s going to be odd obviously.
Fr. Robert: Right so it’s either zero or one. If it’s zero it’s even, if it’s one it’s odd.
Shannon: Well that’s cool.
Fr. Robert: Right, so let’s—
Shannon: So that’s a modulo?
Fr. Robert: That’s a modulo, which we write like this. So if you go ahead and look at my code, again we input a whole number, we ask them to input a whole number. Now you’ll notice here that I went ahead and did a check. I made sure that the variable that they entered was going to be turned into a number right because I want to do math here.
Fr. Robert: Now here’s what it looks like, this right here, this percent sign, that’s the modulo. That’s a mod.
Fr. Robert: So I'm saying a variable called left over is equal to variable one mod two. So variable one divided by two, the left over is going to be put into the variable called left over.
Shannon: That, yeah that totally makes sense and you're using the if else.
Fr. Robert: Right so if variable one was eleven and it mod two, one will be left in the left over.
Shannon: Right so it’s going to say is and odd number because it’s not equal to zero.
Fr. Robert: Exactly, so that’s what this does down here. If left over is equal to zero, then we know it’s an even number, now we also know—
Shannon: So print is an even number.
Fr. Robert: Exactly and we also know that with mod there’s only two possibilities. It’s either zero or one. And so we don’t have to do another if, we just know if it’s not zero it’s got to be one and so therefore we know, go ahead and say it’s an odd number. So let’s go ahead and run this just to make sure that I didn’t mess us this code too. So give me a number.
Shannon: Ah let’see, forty-six.
Fr. Robert: Forty-six, and forty-six is an even number. That one was too easy.
Shannon: That is true.
Fr. Robert: Go ahead and give me a big odd number.
Shannon: Big crazy one, okay.
Fr. Robert: Yeah.
Fr. Robert: Aw man, there we go. Ta-da.
Shannon: It’s an odd number.
Fr. Robert: It doesn’t matter how big or small the number is, it very basic math. You mod anything by two, you’re either going to get a one or you're going to get zero which will give you your odd or your even.
Shannon: That’s pretty cool.
Fr. Robert: Yeah now what I like about this is not just because the code is really, really simple but also because the math is really, really simple.
Shannon: Yeah it is.
Fr. Robert: And this is what we ultimately want to do when you're programming. You want to break it down to the simplest math possible.
Fr. Robert: Because you know big complicated math, it’s fun but where you really get the work done is when you can break it down into the tiny bits and pieces to get stuff done.
Shannon: Oh, absolutely.
Fr. Robert: Yeah.
Shannon: Make it easy.
Fr. Robert: Make it easy. Now we want to take all these concepts and we want to show you how they actually work in code so what do you say we bring in our code warrior?
Shannon: I think we should. Do we have Dale?
Fr. Robert: Ladies and gentlemen, it’s Dale Chase. That’s right from Discovery Digital Networks, he is our code warrior, he’s the man with the plan for Python. Now Dale, we’re covering some basic stuff right now of course. We just covered some for loops, we just talked about if else statements, we just talked about mods. If else statements, I mean you basically don’t have programming without if else statements right?
Dale Chase: Oh no, yeah they're pretty much the backbone to a lot of the code that I write.
Fr. Robert: Exactly, yeah because you need to have smarts right? Software need to have smarts. The if else statements are what give it smarts. It allows it to say “Is it this, yes then do that. If not, well is it that? No well then do this” that’s the sort of stuff that we build into our code so that programs can actually do something useful. If we didn’t have that, programs would essentially be limited to printing things.
Fr. Robert: That’s not great.
Shannon: That’s no fun.
Fr. Robert: That’s no fun.
Dale: No choices would be made.
Fr. Robert: But Dale you know what is fun?
Dale: What’s fun?
Fr. Robert: Whenever you come on and you show us your code. What do you want us to play with today?
Dale: Today I've got – we’ll start with reading from a text file, the contents of the text file. And then we will take what we pull in and turn that into a list essentially.
Fr. Robert: Whoa, whoa, whoa, wait, wait, wait this is brand new. You actually, you want us to take data from outside the program. We hadn’t been doing this before.
Shannon: You could do that?
Dale: Yes this is the fun stuff.
Fr. Robert: Okay now, see this – now we’re starting to get into something that really permeates the real world because previously, all the data was either coded into the program or was just something you entered in real time.
Fr. Robert: What Dale is showing us now, and that’s why I'm bringing this up, is important because it allows your program to act on data outside the program.
Shannon: Ooh, interaction with other fun things.
Fr. Robert: Yeah, so Dale show us how this works.
Dale: All right so here the text file I've got. It’s just a couple of Batman, DC Universe villains here.
Fr. Robert: Actually I was just reading about that stroke last night, ah Slade Deathstroke, okay.
Dale: And so each name is separated by a comma.
Dale: And that’s all I have in this text file.
Fr. Robert: Comma delineated text.
Dale: Exactly, and do the first line here is saying open this file that I had on my desktop that I was just showing you guys in a state of r for reading, for reading purposes only.
Fr. Robert: Right.
Dale: So that gets assigned to this variable here, text underscore file. So it just acts as a reference to this text file on my desktop essentially.
Fr. Robert: Right, this is actually good to point out, there are a lot of beginners who think “Oh this like any other variable so I'm taking the entire contents of text file and putting it into a variable called text file.
Shannon: Oh and naming it text file.
Fr. Robert: Which is kind of true but it’s not really.
Dale: Not quite.
Fr. Robert: Yeah you're creating a pointer, you're creating a reference to where the file is.
Dale: Right and almost sort of an object really because you then have functions that you can use on it. And so I'm using the read function which will read the entire contents as a string—
Fr. Robert: There we go.
Dale: …into this new variable.
Fr. Robert: And now it actually is in a variable.
Dale: Yes, yes.
Shannon: So is that why you included the r on the first line?
Dale: That’s correct.
Dale: That’s right and there are other arguments that you can use for that that I guess we’ll cover later. But, I mean another argument you could use for that, is it w, r w for reading and writing.
Fr. Robert: Yeah, yeah for reading and writing.
Shannon: Oh cool.
Fr. Robert: Yeah so it actually – you're a Linux girl you know this.
Fr. Robert: These are all those little mods that you can add at the end right?
Shannon: Yeah that’s super fun.
Fr. Robert: Is it read only, is it read write?
Shannon: Right, execute.
Fr. Robert: No actually Dale we got a good question in the chat room from M5. He wants to wonder well what’s the limit on the file size, I mean can – how big of a file can you read into a text variable?
Dale: I imagine there is a file size limit on that. I don’t actually know what it is off the top of my head.
Fr. Robert: And you know what I would say to M5, I would say “How about this, break it and report back to us how big of a file you needed.”
Fr. Robert: See, you made work for yourself.
Shannon: I’ll try that too, I’ll try that too.
Fr. Robert: M5, go on to our Google Plus page today and tell us how big of a file you had to push in before your computer said “I ain’t doing that”. All right I'm sorry go ahead, go ahead.
Dale: Totally, so at this point we take our string variable here and then we put split on it which will then use the comma as a delineator.
Dale: And now separates those names that I had comma separated into a list now that we can iterate over.
Fr. Robert: Nice, nice.
Shannon: So you name that foes.
Dale: And do for foe and foes, here’s like one sort of for loop where you can kind of just take the list and then grab each element as it goes over it.
Fr. Robert: Right.
Fr. Robert: The range will be as many entries as there are inside that list.
Dale: Right it’ll go that far and no more. So for foe and foes, so if foes – so each time it goes through, if foe is Joker print “Why so serious?”
Fr. Robert: Hahaha.
Shannon: Oh that’s awesome.
Dale: Else, just print the name of the foe. Just print foe.
Shannon: Oh okay.
Fr. Robert: So in your for loop and you did a simple for loop, you had an if else statement that basically said “I will do something unique if it’s Joker, otherwise I’ll just print the name of the foe” but you could just as easily put a bunch of—
Shannon: Other else statements.
Fr. Robert: …elif, elif statements right. The else if so you could’ve said if Joker, if Deathstroke, if – I forgot, Penguin or whoever else you had in there and each of those could've had their own unique print statement.
Shannon: That’s cool.
Dale: That’s correct, but here it is as it was written and there it is. Joker, actually I’ll pull up the text file so you can see here.
Shannon: Okay so the first one is Joker, since it is Joker it prints out “Why so serious?”.
Fr. Robert: M-hmm.
Shannon: Else if it is not Joker, after that it prints out just the name, Bane, Clayface, Deathstroke and then tells you to exit. Cool.
Fr. Robert: Bane, Clayface, Deathstroke. Now this seems incredibly simple, but of course the reason why you're doing this is because you were showing us, again one of the most useful things a computer program can do and that is to take an external data, do some sort of processing on it right. Compare it to a list, give you some useful analysis. That’s exactly what this does. He could have as M5 said, he could have that huge file. He could have two gigabytes before he starts getting an overload in a 32 bit memory allocation system. And it will search through the entire file for Joker and Joker is the only one that it will replace with “Why so serious?”
Fr. Robert: Right so I mean, again very simple program but I think if you start imagining past just what you see on the screen, you see how useful this can be.
Shannon: Yeah, now what I want to see is him put in a whole bunch of other print out statements, outputs for each of the other names.
Fr. Robert: Now –why so serious? No, sorry. Now I actually Dale, one of the things, if we could go back to your code, one of the things that I’d love for you to explain here, I know people are going to get lost a little bit when they get to the foes equals textfromfile.split function. So you explained it, what it’s doing is it’s looking through the text file, which by the way you read into the variable called text from file. And it’s looking for those commas and it knows those commas are the delineated features so that’s the end of one entry and the beginning of the next. But can you explain, how does this work. So we know that split is a built in function. Foes is what? Foes is just a variable that comes from text file, textfromfile.split function.
Dale: Right, foes is the resulting list from the split function.
Fr. Robert: Oh okay I get it. So textfromfile.split gives you a list built off of the entries that were divided by the comma and then in the for loop you just say from foe in foe. So the number of entries in the list, we compare each entry, see if it’ Joker and then increment.
Dale: Right, yeah – it doesn’t even, it’s not even really looking at the number of entries really, it’s just looking at the list itself and then treating each index as, you know as it’s own string and then just kind of just going over them. It’s not really doing it numerically, it’s just going as what are the contents of this list.
Fr. Robert: Right.
Shannon: So it just keeps going until it runs out of things to look at?
Dale: Right, right.
Fr. Robert: Until it gets bored, until it’s done.
Shannon: Until it gets bored.
Fr. Robert: Now, Dale let me ask you this, we gave them these really, really simple examples of bringing in data from a text file. We gave them really simple examples of for loops and while loops and mods and if else. As our code warrior, what would you like to see out of our audience. As they start coding, as they start taking these lesson and they finally have something really, really juicy to sink their teeth into. What would a challenge be that you think is their level that they could do that maybe challenges them a tiny bit.
Dale: Hmm, that’s a good one.
Shannon: Challenge me, challenge me Dale.
Dale: Let’s see, how about—
Shannon: Give me homework.
Dale: Let’s see, what do I have here?
Fr. Robert: I could see the wheels turning. He’s getting diabolical.
Shannon: I know that I'm going to create a text file for next week.
Fr. Robert: Right so obviously we need a text file.
Shannon: Where does the text file have to go? Does it have to go on your desktop or can you put it on whatever?
Fr. Robert: Well you get to define the path.
Shannon: You can just—
Dale: Yeah you can define the path.
Fr. Robert: If you go ahead and look at his code again—
Shannon: Does it have to be under a directory under a user?
Fr. Robert: Oh it could be anywhere. You just have to tell it. So this is in the directory user, in the directory Dale Chase, in the directory desktop and on the desktop a file called text underscore file.txt. So as long as you know where it is, you can put right there where it’s going to find the file.
Dale: Precisely and so now you've given me the idea.
Fr. Robert: All right here we go.
Dale: All right so use your text file as sort of a configuration file for a program.
Fr. Robert: Ooh.
Dale: So use your text file to load data that your program will then already know to be true like let’s say, hmm, let’s say – how about like, oh what do you call that when – localization right?
Fr. Robert: Right, okay good.
Dale: So let’s say you make a simple calculator but your addition and subtraction string comes from the text file.
Shannon: Comes from the text file.
Fr. Robert: Okay I like that.
Shannon: Cool, I got it.
Fr. Robert: So what he’s saying is, you're going to create a program that is going to go ahead and pull in data from outside. So it’s going to pull in, have a text file full of numbers okay.
Fr. Robert: And you need to do something, using all that we’ve taught you so far on first of all inputting that data, getting into some sort of readable form, then making sure that they're actually integers right? Remember we had—
Shannon: Aha so do some sanitizing.
Fr. Robert: … sanitizing, you got to sanitize. You're probably going to have to run – you're definitely going to have to run a couple of loops here to get all that data in and then you're going to run series of calculations. You get to choose what kind of calculations, you get to choose how it’s going to be outputted but you need to give me some sort of unified piece of data at the end that comes from the text file.
Shannon: So that’s four to five steps.
Fr. Robert: Four to five steps.
Fr. Robert: And actually that uses pretty much that we’ve given you so far.
Shannon: It does, that’s fun, I like it.
Fr. Robert: Dale you are an evil man.
Fr. Robert: Now I know for a fact that you have been sort of itching to get something that you've been playing a lot with and that is using Python with Youtube. What are you going to be showing us in the future?
Dale: Maybe just grabbing some basic data. Using some just HTTP requests or if you want to get into installing libraries, we can try that.
Fr. Robert: Yeah I'm thinking that we’re may—
Shannon: You could do that?
Fr. Robert: We may have to hold off on libraries just a tiny bit.
Fr. Robert: Maybe a later episode but I would love, now that we've given them a way to get external data. Let’s also show them how they could access something from Youtube and I'm going to leave that up to you. So I’ll leave that for you. Code warriors, code mokeys, this is what we've got coming next week. Dale Chase from Discovery Digital Networks, thank you so very much for being with us. Now I don’t know if our TV has a little bit of your beats marked up. Now would be a good time to start playing it so that Dale could tell us what he does, where we could find them, where we could hear the groove.
Dale: Yeah, yeah so when I'm not coding I'm making music with collaborators like Shannon and Dual Core and I've got some joints on bandcamp dchase.bandcamp.com. The joint you're hearing right now is off of my most recent full length Typedef—
Fr. Robert: And by the way folks, you're not mistaken that is Dale Chase and Steve Bomber groovin out.
Fr. Robert: Big money, big money.
Dale: Yeah that’s my bro.
Fr. Robert: And actually, this song that’s playing right now, this is one of the very first songs I heard from your album and it’s just, it’s kind of smooth. This is what I listen to when I'm coding folks.
Shannon: It’s good stuff.
Fr. Robert: Yeah, Dale again thank you so very much, thank you for being our code warrior. Thank you for bringing in these examples each week. Just pushing our audience that much more to improve their code-fu.
Shannon: Absolutely, thank you so much.
Dale: Thank you guys.
Fr. Robert: We’ll see you next time.
Dale: Totally, thanks for having me here.
Fr. Robert: Now, folks that’s a lot of stuff.
Shannon: That is a lot of stuff.
Fr. Robert: That is a lot of stuff.
Shannon: But not as complicated as it sounds.
Fr. Robert: It really isn’t, no, no. That’s what we've been doing, we've been giving you a little piece each week that will make the next piece a little bit easier so hopefully you've watched all the episodes of our Python module. And hopefully you've watched all our episodes of C# because again the build on each other.
Shannon: They do and we explained a lot of these in detail like variables and you know using all these different types of things when we were doing our C# module so a lot of the explanations that we’re kind of skipping over during Python, you can find in that module.
Fr. Robert: Yeah, of course if you're freaking out the best place to go would be to our show notes page.
Shannon: Yes, absolutely.
Fr. Robert: You can find us on twit.tv/coding and what you'll find there is not just all of our episodes, not just a way for you to automatically download each episode into your device of choice but you'll find very detailed notes of what we've covered each and every single week. We put all the examples that Snubs brought up in Snubs compiled. We give you a link to a Github that actually contains the code, actual copy of the code from Dale, which means you don’t have to write this stuff down. We want to give it to you, we want to make it easy for you to start coding.
Shannon: Absolutely, yeah make sure to check out that Github because that link has everything that you'll ever need. Also we are on iTunes, still. Apparently we’re still one of the top podcasts, which is crazy.
Fr. Robert: Go figure, I don’t know what’s going on.
Shannon: People are watching this show, it’s so weird.
Fr. Robert: We like [?]
Shannon: And if you guys use Android too, I just figured out that we are on Dog Catcher as well. You can search for us on there too. Just search for Coding 101, we’ll pop up.
Fr. Robert: Doge, doge catcher.
Shannon: So make sure to give us a rating.
Fr. Robert: Yeah, please do. And also, you know as long as you're going to give us a rating why not jump in to our Youtube page as well at Youtube.com/twitcoding101. Again you'll find all the episodes—
Shannon: Look we have 1700 subscriber. Yay.
Fr. Robert: Exactly, you know more important than all those we’re also going to – you'll get a nice entre into our G plus page, into our community page. It’s all something that we’re trying to create. We don’t know exactly what we’re going to be doing with it yet but join, be part of the community and help us figure it all out.
Shannon: Absolutely and again that was gplus.toc/twitcoding101 and you can see all those different comment that we've been receiving from everybody who’s been sharing their code. Very, very helpful for each and every one of u.
Fr. Robert: And be sure to send us those coding examples because you've seen, we use them on the show. You know, it’s like pinning up your homework on the refrigerator.
Shannon: A plus.
Fr. Robert: Yes and oh by the way if you're not into the G Plus group, I get it, I understand. You don’t want to be in yet another social network but you could also find us on the Twitters. That's right, find me at twitter.com/padresj.
Shannon: And I'm twitter.com/Snubs.
Fr. Robert: When you follow us, you also get a free toaster.
Shannon: Free toasters. I am a Cylon.
Fr. Robert: But you know I think that’s about it. You know it’s been a full week. We’ll see you next week with some brand new stuff including some Youtube integration.
Shannon: Youtube fun.
Fr. Robert: Go ahead and play with your parsing. Bring those text files and show us your code.
Shannon: You got a week.
Fr. Robert: You got a week.
Shannon: Do it.
Fr. Robert: Until next then, I'm father Robert Ballecer.
Shannon: I'm Shannon Morse. End of line.
Fr. Robert: End of line.