Download and watch the episode here:
Coding 101 16
Shannon Morse: Today on Coding 101 we are breaking it down. We’re going to show you how to output to a file and we’re getting back into lists.
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.
Father Robert Ballecer: This episode of Coding 101 is brought to you by Shutterstock.com. With over 30 million high quality stock photos, illustrations, vectors and video clips, Shutterstock helps you take your creative projects to the next level. For 20% off your new account, go to Shutterstock.com and use the offer code CODING514. And 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 10% off, go to squarespace.com and use the offer code CODING.
Fr. Robert: Welcome to Coding 101. It’s the show where we bring you into the wonderful world of the code monkey. I'm father Robert Ballecer.
Shannon: And I am Shannon Morse and for the next 30 minutes we are going to get you all learned up on everything you need to know to be a Python code warrior.
Fr. Robert: I like getting learned up.
Shannon: Learned up, that's right.
Fr. Robert: I need to be learned.
Shannon: I just made up a word.
Fr. Robert: Now we've been getting kind of complicated the last couple of weeks, that kind of messed people up.
Shannon: We did, that whole Youtube API thing totally threw me off. I was very confused.
Fr. Robert: Yeah, yeah so what we're going to do for this episode is we're actually going to step back a little bit because we've been getting emails from people who are saying “I like the Python module, it’s very accessible but maybe if you could step through some of the main topics so we can catch our breath.” And you know what we want to do what you need.
Shannon: I think that's a great idea. So last week, we discussed the Youtube API but we also discussed sorting as well.
Fr. Robert: Yes.
Shannon: So I wanted to give my very short and very easy to use example just so you guys have a very brief review of sorting.
Fr. Robert: Right.
Shannon: So I created a new IDLE code. I’ll show you my code, so this is very similar to what Padre showed us last week where you have a variable called sort numbers and then it has a whole range of numbers. And these are totally jarbled up, they aren't from you know minimum to maximum or anything like that. To get them sorted though I used this code, Sortnumbers.sort, parenthesis and then a semicolon.
Fr. Robert: And we should note that that is a built-in function, a method in Python.
Shannon: It is.
Fr. Robert: Sort with the little parenthesis, all that means is that your using something that's already built into Python so you don't have to code it yourself.
Fr. Robert: In this instance, you've created a list called sortnumbers and with those entries in the list and then the way that you invoke the method is you type in the name of the list, period and then the function you want to write on that list.
Shannon: And this is my function.
Fr. Robert: Boom.
Shannon: So after I hit that, I say for these entries in the range from zero to five, so zero, one, two, three, four, five, print sortnumbers entries. So this is going to print the sorted range from zero to five for me.
Fr. Robert: Right.
Shannon: And then it just says press enter to exit. So when I close this, and I’ll go ahead and open it up, it automatically prints from two all the way up to 74,533, and then it just asks you to exit. So you'll see that all of the numbers are automatically sorted from the smallest to the largest.
Fr. Robert: And as we mentioned last week, this is one of those fundamental things that you absolutely need to do. I mean manipulating data inside of your program is what you program for, unless you're actually doing something useful then you're not really programming. So sort is a big part of that.
Shannon: Would you like to see a couple of viewer examples?
Fr. Robert: Yes I would.
Shannon: Awesome, so first one we got was from Daniel, this is a book sorting one. This is a Python code that he created that will take a whole bunch of names of books, page numbers, titles and authors and it will sort them depending on how you want it sorted by either page numbers, title or the author name. So if I open up his code, I'm going to go ahead and run it first. So it’s going to take a text document which I have downloaded right here, it’s called books.txt and it asks you “How do you want sort this?” And I'm going to say I want to sort it by titles, so number one and then hit enter, and it’s going to sort them by title. So you see that it goes “The following was written – also it outputs new files so we’re going to get into this later—
Fr. Robert: We're going to talk about – yeah, there's your spoilers.
Shannon: Yeah so I'm not going to show you how exactly to do that quite yet but you'll see down here it starts from b, build your own website blah blah blah, all the way down to the very last ones. And then I can also sort again by author, hit number two. So it’s going to sort again by author and number three by page count. When I do that you'll see a different listing of the books. Now when I exit out of here, you can see these new text files as well by author, by page count and by title. If I open books it’s very simple, it’s just a list out of his books. And then he has separated page numbers, by authors and everything else by this little character right here. Ta-da.
Fr. Robert: Yeah, and again that's a fundamental part of programming which is if the data only works in the program and there's no way to get either external data in or internal data out then it’s not so useful. So what we've seen is we already have the method for puling data into your program we're going to show you today how to get it out.
Shannon: Exactly, yee, well before that we have one more example for you.
Fr. Robert: Let’s do that please.
Shannon: This one is from Lionel. So this one takes some code, it pulls some API information from two sources and then it sorts that into a listing for you. So he’s also taking the Youtube API that we discussed last week—
Fr. Robert: Which freaked some people out.
Shannon: …you can tell that Lionel is quite more advanced than some of us are, like me I don't fully understand APIs but he got on it and he was like I can totally do this. So when I run his code right here, double-click on that. So it’s pulling some Python API data and then it gives you weather information. So it’s going to say – let’s see, sort Telize API data. So I'm going to hit one, and then press enter to see the full unsorted API from Telize.com. So he’s pulling API information from Telize.com. When I hit enter, it gives you a listing of all the information about yourself. So it gives me, you know Petaluma, CA, that's where we are, Comcast cable communications, blah blah blah. And then press enter to see that data sorted by key, by value and then I can hit exit. Bam, pretty cool right?
Fr. Robert: Very awesome but I'm going to make this plea, chat room you just saw one of our IPs, please don’t hit it. I know you're going to want to, but I will say this, maybe we have a Know How or project on building a honey pot so maybe you do want to hit it, I don't know. Again that's the sort of stuff that I love. Obvious he has way more experience than the programs that we've been bringing on to the show, but that's the level of functionality that you can get out of an API.
Fr. Robert: That's what we've been trying to tell people, which is any time you can reuse code, or reuse a package that someone else has written so you that you don't actually have to write it, you can just throw it some variables and have data come back, that's always a great thing.
Shannon: I agree, and I do want to say thank you both to Lionel and Daniel for sharing their code with us, and to everybody else that shared code in the past week. We also had Jerry, Eric, Patrick, Darryl, and Neverine also share their code in the Google Plus community.
Fr. Robert: Moot.
Shannon: So unfortunately we don't have time to share all of them but each of them was really good.
Fr. Robert: We’d love to, yeah. You know I've noticed that on the G Plus community, there been a lot of activity of people sharing code.
Shannon: There's been tons, it’s so exciting, I love it.
Fr. Robert: I love it, yeah. And that's actually another reason why you want to be part of that G Plus community.
Fr. Robert: Even if you're freaking out and you don't have any code to contribute to the community yet, take a look at the examples that people are writing. Especially people who know way more than the level that we’re teaching because—
Shannon: Yeah absolutely.
Fr. Robert: …and we talked about this in the first module, one of the best ways to learn programming is to get in there and just start poking around. Get the code from someone else that works and figure out what you can change.
Shannon: Break it and then figure out how to fix it.
Fr. Robert: Break it.
Shannon: So I'm currently working on trying to sort my list, my wedding list into an alphabetical order. I haven't quite figured it out yet but I'm really, really hoping I can get that fixed by next week so I can show you guys.
Fr. Robert: Yeah, yeah and again you know anytime you can do something that's useful to you—
Shannon: Yes I agree.
Fr. Robert: …that's that little light bulb moment.
Shannon: So speaking of useful—
Fr. Robert: Uh-hmm.
Shannon: …I hear there's a pretty sweet sponsor.
Fr. Robert: Yeah, yeah so we're creative professionals right?
Shannon: We are, yes.
Fr. Robert: I mean some of us more than other, I can't design picture to save my life but I do like making content and one of the things about making content is you need to assets to make that content right, which is why we welcome Shutterstock to the Coding 101 family.
Fr. Robert: Now what do you get with Shutterstock. With Shutterstock you get, well a multitude of options for the type of content that you need in your project. Whether it’s for your website, for a publication, for an advertisement, or for a video or any other type of project you can choose from over 30 million high quality stock photos, illustration, vectors and video clips all in one site. Now how does this work, you may ask? Well is works because Shutterstocks sources images from around the world and puts them at your fingertips. Many contributors to Shutterstock are professional photographers and artists, they do this for a living. Shutterstock reviews each image individually for content and quality before adding it to its library. They don't want to give you stuff that's just garbage. They don't want us to have to sort through pages and pages and pages of chaff to find the one stock image that you want. Rather they help to curate it for you and you can download any image in any size and pay only one price. Shutterstock gives you the images you need to bring your creative projects to the next level and they make it easy. Shutterstock has sophisticated search tools so right now our TD Bryan is looking for hacker and these are some of the images that are coming up inside the Shutterstock search tool. With these search tools you can search and drill down by subject, color, file type, gender emotion and more. For example, I want to look for programmer but I want to specifically look for Python programmer. He entered in those search terms and believe it or not, Shutterstock was able to give me just the images, just the assets that are associated with programming in Python. It sound like magic but it’s just really that good. It’s a really accurate drill down search engine. Now Shutterstock has an award winning iPad app which mean you can search on the go for your mobile device and use it to display images during presentations. They also have shareable Lightboxes which means you can share images to a Lightbox gallery then access them anytime and share them with other team members. Shutterstock really is a global marketplace. They have multilingual customer service in more than a dozen international countries and full time customer support throughout the week. Now today, you know Bryan just showed you some of the images that we found for hacker and Python programming but I'm going to throw a curveball at him. I want you to look for priest. Watch what happens when you type in priest? Let’s see what comes up, actually we probably should've checked this ahead of time but Bryan is it good stuff?
Shannon: Spell it right Bryan.
Fr. Robert: You have to spell – do you know how to spell it? There we go. Well what happens when we type – another curveball here, type in Jesuit priest. I want to see if we have a subheading for us. Do you know how to spell Jesuit? Oh my goodness, okay that's – I didn't think that was actually going to work but here you go. They're actually showing some of the images from Jesuit Institutions around the world. This is the kind of magical search that you get when you use Shutterstock. Folks I tell you, it’s one of the best ways to get assets, it’s the best way to get photos and videos for whatever project you're working on, it’s Shutterstock. Now you can try Shutterstock today, by signing up for a free account. There's not credit card needed. You just start your account and you begin using Shutterstock to help imagine what your next project will look like. And save favorite images to that Lightbox to review later. Now once you decide to purchase, you can use offer code CODING514. That's C-O-D-I-N-G five one four and new accounts will receive 20% off any package. That's Shutterstock.com and for 20% off new accounts, use the offer code CODING514. And we thank Shutterstock for their support of Coding 101.
Shannon: I'm ready.
Fr. Robert: You ready?
Shannon: Are you ready?
Fr. Robert: I'm ready, are you ready for this?
Shannon: Oh I'm totally ready for this.
Fr. Robert: Dun dun dun dun. Oh sorry we just had to do that.
Shannon: Dun dun dun dun. We don't want to make everybody leave.
Fr. Robert: That was a little bit of a dance. Okay so we're going to—
Shannon: Dance off.
Fr. Robert: Dance off, dance – no.
Shannon: So we have a lot of information to get through today.
Fr. Robert: We do, we do because what we're going to do is we're going to back up a little bit and for someone who might be drowning in a lot of the firehose type—
Fr. Robert: Yeah, exactly, we’re going to take you through some of the steps and we can't obviously take you through all of the episodes that we've done.
Shannon: Of course.
Fr. Robert: But let’s take you back and rewind the clock so that you can at least get—
Shannon: Do a little bit of a review. All right, cool.
Fr. Robert: Yes before we get into pushing data out an external file. Now the first thing we have to start with of course are variables. Now we remember variables right?
Shannon: We do, yeah we've been talking about those since the beginning of time.
Fr. Robert: Yeah, well beginning of programming. Variables are the things that make programs go.
Fr. Robert: If you don't have variables, you don't have a place to store values, you don't have a place to be able to change values, right?
Fr. Robert: And remember when we did the first module with C#, variables had to be defined.
Fr. Robert: You had to tell it at the beginning.
Shannon: You had to tell it whether it was a number or a letter or a string or whatever.
Fr. Robert: Or a string, exactly. We talked about in Python how it’s different because you use dynamic variables.
Fr. Robert: Which mean that the type of variable is dynamically allocated as you put data into that variable.
Shannon: Which is really cool so you don't have to worry about actually defining it.
Fr. Robert: Right, so like when we were making our C# programs, what we would typically do is you'd see at the top, we’d list off all the variables that we're going to use.
Fr. Robert: It’s type integer, type float, type – don't do that in Python. In Python you can just conjure variables out of nowhere, just give it a name.
Shannon: It’s like magic.
Fr. Robert: It’s like magic and when a value gets pushed into that variable it will automatically know what type of data it’s dealing with.
Shannon: I love that. Why can't every language be able to do that?
Fr. Robert: Well I mean, they could. And actually most of the newer languages do but you got to remember, when we're talking about the imbedded programming base, there's a lot of us who grew up with the old style.
Shannon: Yes that's true.
Fr. Robert: So don't forget us. You know we're still here.
Shannon: You old people.
Fr. Robert: Old people.
Shannon: I'm just kidding.
Fr. Robert: Okay so now we've got variables, now I just want to get it out of the way. Let’s show you really quick what it looks like to play with some of this data. This was one of the very first functions that we played with. This is called the raw input function.
Fr. Robert: Now all it does is it allows you to wait for user input.
Fr. Robert: After printing out whatever is in between parentheses here. So I could've put the name of a variables without the quotation marks and it would've printed the variable, in this case, all it’s doing is printing out the string, please type your name. Now remember how I said you could dynamically assign variables, that's what it’s doing right here. I haven't told Python what variable one’s going to be.
Fr. Robert: All it’s doing is it’s waiting for information to be pushed into it and then it will dynamically assign it. In this case, variable one is going to be filled—
Shannon: My name.
Fr. Robert: With, yeah with whatever raw input finds.
Fr. Robert: And then it’s going to print back whatever was in variable one.
Fr. Robert: So if I run this, it should look just like this. So my name is Padre, and if I hit enter it should type back My name is Padre.
Shannon: Yay, now I do see an extra space in between is and Padre so could you have deleted that extra space by just adding it at the end of your raw input?
Fr. Robert: Absolutely, absolutely yeah I added one just because I realized it was all squished together but—
Shannon: Got it.
Fr. Robert: …yeah you know in the code, you can make it come out any way you want.
Fr. Robert: So that was one of the first functions that we played
Shannon: Nice and easy.
Fr. Robert: Nice and easy, no muss no fuss. Nothing hard about it. Now the second function that we really played with was the isdigit. You may remember it back in episode 3?
Shannon: I do.
Fr. Robert: Yeah we started talking about sanitizing data which was perfect because that happened during the heartbleed, right.
Shannon: The heartbleed.
Fr. Robert: Which again, of course it was more complicated with heartbleed—
Shannon: Yes it was.
Fr. Robert: …but essentially that's what happens when programmers don't make sure that they're receiving the data they think they supposed to get.
Fr. Robert: Right, so if you look at this, this was just a variation of the – actually no, I think this is the exact code that we used for episode three. We used this function called isdigit. And all isdigit is, is it takes a value. In this case it’s taking the value of phones and it’s saying “Is it a number?”.
Shannon: Ah yes.
Fr. Robert: If it’s a number—
Fr. Robert: Then yes, go. If not then no it stops.
Fr. Robert: And in this particular case, this was a program that Dale wrote. We start off with a variable called clean set to false.
Fr. Robert: Then we have a whileloop and a whileloop will continue to run as long as clean is equal to false. So in other words, this loop that we're about to write here will continue to run until something changes the value of clean to true.
Shannon: Got it, okay.
Fr. Robert: Which is what you want to do right? This is sanitizing.
Fr. Robert: Stay in this loop until I get what I expect.
Fr. Robert: Okay, then I have these two inputs. Phones and Laptops, how many phones have I broken? How many laptops have I dropped?
Shannon: And here we're talking about variables.
Fr. Robert: Right, there's the variables again. So I enter in two different numbers, now this if statement, what is does is it uses the isdigit function to say “If the variable of phones is a digit and if the variable of laptops is a digit then clean equals true” which means the whileloop stops.
Shannon: Got it.
Fr. Robert: Right.
Shannon: It totally makes sense.
Fr. Robert: And then it goes on to the next line which just says Print and then we use another function called int, which makes sure it turns it into an integer. Make sure that it’s actually a number that it can play with. Phones and Laptops and there we go. So if I run this, this is what it looks like. So how many phones have I broken? I've never broken a phone. How many laptops have I dropped?
Fr. Robert: Oh like 200. My bad, boom, okay. And there we go.
Fr. Robert: So again, nothing startling, nothing earth shattering but when you start adding it up to all the other ways that we've learned you to actually modify data, it’s pretty cool.
Shannon: Okay, yeah I got it so far.
Fr. Robert: Good?
Shannon: What's next?
Fr. Robert: Good, what's next I believe we’re going to be covering lists.
Fr. Robert: So this was us getting into structured data. And the reason why structured data is important is well when we're dealing with variables that you know in the past we've just thrown a bunch of variables together right?
Shannon: That's true.
Fr. Robert: Which is useful but wouldn't it be nice if somehow I could group together all the similar variables into one area.
Shannon: That's so much more useful.
Fr. Robert: Right, for example let’s take your wedding list right?
Shannon: The wedding RSVPs.
Fr. Robert: Yeah RSVPs, I could have a thousand variables for the thousand different entries or I could have one list that says “Wedding Reservations” with a thousand entries in it.
Shannon: So much easier than typing them all in separately.
Fr. Robert: That's what we work with. So let’s go ahead and go to my computer Byran and what you see here is this is the one that we used in the episode. I have a list called C101team.
Fr. Robert: And then I grouped in the list, all the people who are associated with C101.
Fr. Robert: We've got me, we've got you, we've got Dale Chase and we've got Cranky Hippo.
Shannon: Perfect, okay.
Fr. Robert: Now so this will create a list called C101 team with entries zero to three because we start at zero.
Fr. Robert: So there's four entries, with the values of PadreSJ, Snubs, Dale Chase, etcetera. Now this, the print as you can see just – you print out the name of the list plus the index that you want to output. And so when I output this, it looks just like this.
Shannon: PadreSJ would be zero.
Fr. Robert: Bam.
Fr. Robert: And again I could space this out a lot better I really wanted to.
Shannon: I would be one, Dale would be two, Cranky Hippo’s number three.
Fr. Robert: Boom, right?
Shannon: Got it.
Fr. Robert: So list, really there's nothing complicated there. The only reason why we're talking about it is because we want to always have a better way to organize our information and lists are a great way to do it.
Shannon: Yeah absolutely, I love this.
Fr. Robert: Structured data, we love it folks, we love it.
Shannon: Me too.
Fr. Robert: All right, so now we've got variables, we've got lists, we've got a way to get information into the program, we've got a way to make sure it’s what's we expect, right?
Shannon: Yes. What's next?
Fr. Robert: Remember the decision makers?
Shannon: Oh yeah.
Fr. Robert: Yeah so even if we got—
Shannon: Ifs and else and loops.
Fr. Robert: Ifs and else and right – so let’s talk about those loops first because we've already done a little bit of it when we looked at one of the previous examples. This is one of the easiest loops you can write. This is a whileloop and this looks almost exactly like the whileloops that we did in C#. I have counter is equal to zero, so it’s a variable called counter.
Fr. Robert: I set it to zero.
Shannon: Got it.
Fr. Robert: The whileloop will continue to run as long as counter is less then five.
Shannon: Is less than five.
Fr. Robert: Then inside the loop, and remember we know it’s inside the loop because it’s—
Shannon: It’s indented.
Fr. Robert: Precisely, right whitespace matters in Python folks. It’s indented so I know each time the whileloop runs, counter is going to become counter plus one.
Fr. Robert: So it’s just an increment, increment by one. And then print counter. And so when I run this program, it get this. 1,2,3,4,5.
Fr. Robert: Right?
Shannon: Yeah exactly.
Fr. Robert: Very easy to understand, that's what a whileloop does. Now let’s go over to forloops. Forloops are – they're slightly more complicated but not a whole lot. It looks different then the loops inside of C# because my forloop at the very beginning I know exactly how many times it’s going to run.
Shannon: That's true.
Fr. Robert: Right it gave it two arguments, where it starts and where it stops. So it’s saying for counter, and I'm declaring the variable here, counter, while the counter is in the range between zero and eight, print the counter.
Shannon: So I don't remember, will it run eight times or nine times?
Fr. Robert: Well, let’s find out. So what happens when I run this program I get something that looks like this.
Shannon: So eight times.
Fr. Robert: Eight times, right.
Fr. Robert: And that's because, remember when we talk about pretest and posttest loops, it’s going to run when it’s zero.
Shannon: Right, yeah.
Fr. Robert: Because it pretests it right?
Fr. Robert: And then it’s going to continue to run but that last time that it runs through, when it increments the counter to eight, it’s going to say “Oh I'm no longer in the range. I stop.”
Fr. Robert: Yup.
Fr. Robert: I actually confused a lot of people. All right, so we've got the loops, now let’s get into the if else statements.
Shannon: Yay. These are super handy.
Fr. Robert: These are – well I mean, the logical tree, remember from module one we talked about taking a problem out in the real world and turning it into computer language, well that's really if else statements. Computers like to think in binary, either yes or no. and so if else statements allow me to say yes or no. Is it this? Is it that? Is it this? Is it that?
Shannon: It’s kind of like turning on a switch, it’s either on or it’s off.
Fr. Robert: Right exactly and that's really what an if else statement looks like. And so if you look at my console, I had a very simple function that I wrote here. I have a whole number a, I have a whole number b and then I have a series of if else statements. Now I have if, elif which means elseif and else.
Fr. Robert: So this is all the bases. There's now possible number that you can enter that's not going to fall in one of these three categories.
Fr. Robert: So let’s say I put in the number two and the number four. So I a is equal to two and b is equal to four. Well then I get to the first statement, is two less than four? Yes.
Fr. Robert: Then it just prints, a is less than b and it stops.
Fr. Robert: Okay now let’s say I put in four and two. So a is equal to four and b is equal to two. It’s going to go to that first statement, “Is a less than b?” So in other words “Is four less than two?”
Shannon: And it’s not so it’ll move on.
Fr. Robert: It’s not, it’ll move on. Right so it’ll ignore the print that's associated with the if and it will move on to the elif, which is “Is a equal to b?” so “Is four equal to two?”
Fr. Robert: And the answer for that is, no.
Fr. Robert: Four is not two, right? So it’s going to move on to the next one and the next one is sort of the catch all. Well if it’s not less than than b, if it’s not equal to b then it must be greater.
Shannon: Then it must be b is less than a.
Fr. Robert: Exactly and that’s what it will print. So let’s go ahead and make sure that works. So if we run this, I'm going to enter in five and four. There we go.
Shannon: B is less than a, perfect.
Fr. Robert: Four is less than five.
Fr. Robert: And that's – again it’s very simple…
Fr. Robert: …but very handy, especially when start nesting those things.
Shannon: I agree.
Fr. Robert: Because then you can make your computer kind of smart.
Shannon: I love all this review. This is perfect. It’s bringing back everything.
Fr. Robert: It brings back stuff and it’s really a good way to sort of lead up to what we're going to get to because—
Fr. Robert: You know what I'm actually going to skip sort because you covered sort really well.
Shannon: Oh thank you.
Fr. Robert: Right, yeah you run it and it sorts what's in the list.
Fr. Robert: So let’s go ahead and talk a little bit about external data. Specifically right now, let’s talk about bringing data into your program. So here’s what we've got, we have a text file, a function here, so the variable is called text file and I am going to set it to open. And then I'm going to give it the path file that I actually want it to open.
Fr. Robert: And I want it to open a file called nunyobiz.txt. Now you will notice that right here there's this r. That's actually important, we glossed over it previously because it wasn’t important, now you need to know it.
Shannon: That's for read.
Fr. Robert: Read, right exactly. So I'm going to be reading this. I'm going to open this text file for reading. Now I'm not actually – in this actually putting the test file into the variable called text file, what I'm doing is I'm setting the pointer. So I'm telling it “Go ahead and find out where in the memory we're reading this file in.”
Shannon: Ah okay.
Fr. Robert: The next part here, now I'm saying the textFromFile is equal to text_file.read, that's actually taking what's in the text file—
Shannon: So it’s giving you that name, that text from file name.
Fr. Robert: Exactly, exactly and it’s going to read it into that string variable. So that entire file is now located inside this variable.
Shannon: Got it, okay.
Fr. Robert: All right, and now I'm going to do a – well I think I wrote, oh yeah I'm using a sort list so I'm reading it into a array and then I'm just outputting it. So what we have here, let me show you what the file looks like. Nunyobiz is just filled with a bunch of numbers, right. So if I go ahead and run this now, it’s just going to read out whatever's in that file.
Shannon: Okay, I got it.
Fr. Robert: Yeah, now that's not so useful because it’s just doing a data dump of what's in the file but the important thing is that data is now in the program so if I wanted to say sort it or play with it—
Shannon: Got it.
Fr. Robert: …or mess around with it, boom.
Shannon: Oh mind blown, I just think I figured out why my program wasn't working. I wasn't inputting the information before trying to sort it.
Fr. Robert: Yeah, yeah.
Shannon: I was just trying to sort it while bringing in data.
Fr. Robert: You were trying to sort that, the thing that you – yeah exactly. You have to bring it into the memory first then you can sort it.
Shannon: Poof, huh I figured it out. Yes.
Fr. Robert: Boom, all right so we got that for next time.
Shannon: Now I want to do it.
Fr. Robert: Now, okay here's what we're going to be covering today.
Shannon: Oh boy.
Fr. Robert: We need a way to get the data back out to a file. It’s cool to have data coming in but unless I have a way to actually hand it off to someone without giving them my computer—
Fr. Robert: …it’s kind of useless right?
Shannon: I agree yeah.
Fr. Robert: So remember how I told you that that are was going to be important in the open?
Fr. Robert: I'm doing the same thing here, so this is almost the mirror—
Shannon: You have a W.
Fr. Robert: I have a W so I'm writing, okay.
Fr. Robert: So what this is going to do is this is going to ask me to type a test string. then it’s going open a file but it’s going to open it for writing, not for reading and then it’s going to push that string into the external file and then it’s going to close it.
Shannon: That's awesome.
Fr. Robert: So if I run this, and let’s go ahead and do this, Hello Snubs.
Fr. Robert: All right, it ends the program, now if I go ahead and jump over to - oh my gosh I have so many tabs open right now.
Fr. Robert: Where did it go, hold on.
Shannon: File exported.
Fr. Robert: There you are. Oh no wait, bam okay. Now if I go to the right, which was the file that I created. Hello Snubs.
Shannon: Huh, that is so cool.
Fr. Robert: Right, and again super simple demonstration but what we're going to do now is we're going to show you how you could take data in from a file, do all those things that we just you how to mess with data right. So raw input, playing with variables, doing if else statements, whileloops.
Shannon: So you're telling me I can take those wedding RSVPs that huge list that I created, I can input all that information, I can tell Python to sort it and then just output all the sorted information back out so then in can use it in an Excel spreadsheet or whatever I need.
Fr. Robert: Boom, you can use an Excel or anything else.
Shannon: I can send it to my vendors. Aw man, I'm so excited about this.
Fr. Robert: Yeah, I know it’s good stuff which means we’re going to bring in the code warrior, but before we bring in the code warrior, you know what Snubs this is all cool and this is great. You know this is a nice way to get data out of a program but once you get the data out of a program you really need a place to present it. You want to place to share – like those invitations, wouldn't it be nice to have a website where you could push that data to.
Shannon: I agree, I totally agree and I like putting information in websites. In fact I used to do that when I was a little kid. I would make little anime websites but they were so ugly. It’s a lot easier now.
Fr. Robert: It’s easier but you know what, I just don't know who I should choose. There's so many out there.
Shannon: I'm thinking you should choose Squarespace.
Fr. Robert: Oh all right.
Shannon: Have you heard of Squarespace.
Fr. Robert: I think I have but tell me more.
Shannon: This is the site you want to go to to create your own domain, your own website with a unique and beautiful feel. If you've ever used Squarespace before, you know what I'm talking about. They're constantly improving their platform. They have new features, new designs, even better support than you've ever seen before and beautiful designs. So when you create a website on here, you can choose over 25 beautiful templates and they're gorgeous templates, I mean look at those they're beautiful, for you to create your own personal or business site. In fact I've actually shared this, Squarespace with a lot of my friends who are you know starting to be their own entrepreneurs. They want to create their own site to say hey “I'm a graphics designer. I want to create a website to show off what I can do.” So they go to Squarespace and they create something, bam in like a couple of hours they're done. They're ready to go. It also has a really nice mobile experience too, Squarespace has developed templates with responsive designs which means that your site automatically restructures to look great on any device.
Fr. Robert: That's super important.
Shannon: Have you ever been to a site that looks totally janky on your smart phone? I have.
Fr. Robert: Your kind of just scrolling like “Come on, I can't see anything, oh.”
Shannon: Oh I can't pinch, what's going on here? Oh I can't even see what that text says. Or you double-click and it just goes [makes noise].
Fr. Robert: Exactly, yeah. Those are the worse. I mean, it doesn't matter how good your website is, if it doesn't automatically adjust for the device on which it’s being used, your laptop, your tablet, your phone, it just looks amateur.
Shannon: And then I don't want to go there. Well Squarespace does all that work for you so you don't even have to worry about fixing it yourself. They do all the code on the back-end for your smartphone, for your tablet or your computers so it looks great anywhere. And it’s easy to use. So if you want some help, if you have any questions or anything like that, Squarespace has live chat and email support 24 hours a day, seven days a week. Plus there is a completely redesigned customer help site for easy access to self-help articles and video workshops.
Fr. Robert: Bam.
Shannon: Also, hosting is included, haha. Squarespace takes care of the hosting with a distributed network that does not go down. And I'm telling you folks, even with like our sites that I've used before and people that have signed up with Squarespace that I've told about, their sites never go down no matter how many people go to it. It’s also inexpensive, I mean you're not going to find a good deal like this. It starts at 8 bucks a month and it includes a free domain name if you sign up for a year, which personally I did that and it’s awesome.
Fr. Robert: Yeah.
Fr. Robert: Do it now.
Shannon: Do it now.
Fr. Robert: Now.
Shannon: Do it now.
Fr. Robert: Well actually, you know what we should do now, we should—
Shannon: I'm thinking we should say hello to our code warrior.
Fr. Robert: I think so, let’s welcome back to the show Mr. Dale Chase. He is the engineer slash artist extraordinaire from Discovery Digital Networks.
Dale Chase: I am here. Get to the coding.
Shannon: What’s up?
Fr. Robert: Whoa.
Shannon: [laughter], get to the coding.
Fr. Robert: Oh I don't know what's going on with that.
Shannon: Do it now.
Fr. Robert: Haha, do it now. Go, go now Dale we gave you a little bit of entre here because you know we went back to some of the stuff that we've done. Obvious you've done a great job in pushing forward the folks.
Fr. Robert: They kind of freaked out a little bit on the Youtube API but I think they understand why it’s important. So we've teased it, we've showed them what it basically looks like to push data out to an external file, but now you're going to give us the real deal, yeah.
Dale: Yeah how to write that data to a file, which is super handy. So actually I have some – what I'm going to do is I'm going to bring in some song lyrics and then change their case from all upper case to lower case and then save that new string of song lyrics to a file.
Fr. Robert: And this is exactly what we've been talking about, which is the ability to take something into the program, process it, do something useful and then give back a useful file.
Shannon: This is perfect.
Fr. Robert: Okay so the code for this must be like pages and pages long right? I mean it’s got to be tremendous.
Dale: I think it’s like seven lines I think.
Fr. Robert: I don't believe you, show us.
Dale: It’s like eight, it’s like ten lines.
Fr. Robert: Wow, that's pretty good. All right so we've seen that first line before, so the path to file is, that's just you're setting the pointer. You're saying “Okay this is where the file actually exists.” Then you're going to go ahead an open – why do you have to open a file? I mean I know some people have been asking this before. We went over open w and open are, so open reading and open writing. What exactly does that mean? Why do I have to do that step?
Dale: It just kind of gets it ready to be worked with. You're letting the computer know that you want to bring this file into memory because you're going to do something with it at some point.
Fr. Robert: Right. Oh actually, on quick thing, can you go back to my computer Bryan. We have to show people this because they kind of freaked out the last time. When you see this in the example code folks, this is path to file. So the people who are writing me and saying it doesn't work, that's because you need to put your own path.
Shannon: Yeah I can show you.
Fr. Robert: This is a local path, right.
Shannon: So for example, if you still have my computer, if I go up here and I click on this little folder icon right up here it gives me my own path. So I just copy that and then include at the very end the name of my file that I want to include. So if I wanted to open in Python booksbyauthor.txt, I would just include that to the end and just copy and paste this entire line up here.
Fr. Robert: Right so just remember that when you're writing this for yourself, doing use our paths because our paths will do nothing to your computers. I'm sorry Dale, I had to cover that just because I got like four emails from that.
Dale: No it’s fine, and also if you're on a Mac, if you just drag that file into the terminal it’ll also give you the path to that file.
Shannon: Oh that's awesome.
Fr. Robert: Right, exactly.
Shannon: What? Hahaha.
Dale: But if they're are spaces in the file, you'll have to remove any slashes that will come from that.
Fr. Robert: And that’s actually pcguy8088 in the chat room, he says exactly. Normally you wouldn't hard code that path. That path would be assigned to a variable and then the variable would be – it would like read from the directory or it would use a function to actually figure out where the file is.
Fr. Robert: Yeah, but we're hard coding it because we want to make this easy for you.
Dale: Totally, I guess it’s worth mentioning because that's kind of why I've got this in all caps here because it’s meant as to be constant, you know something that won’t change throughout the program.
Fr. Robert: Right.
Dale: So that's a good point to possibly touch upon at some point. So yeah, you just wanted to just jump over to – I guess I’ll just kind of explain how this works. So we're opening the file, getting it right to be red and assigning it to rhymesFile and then I'm reading the contents of the file into this variable rhymes and then I'm closing the file—
Fr. Robert: Okay now there's another question, people ask this about our program when we did the external data segment which is “Why didn't we close it before? What's important about closing a file? Why we use that function?”.
Dale: It’s good to let the computer know that you're done using it for this case right now. You don't want to accidentally do anything with it or you don't want the computer to accidentally do anything with it. You know you want to just close it and make sure that it is good and wrapped up.
Fr. Robert: Yeah it’s good programming folks and if you open a file you really should close it. We didn't do it last time, that's actually – I got lazy sorry.
Shannon: Yeah we didn't do it, sorry.
Dale: And so then I'm using this built-in function here lower which will take the contents of the string and set them all to lower case.
Shannon: Oh so—
Dale: And then I'm assigning – yeah.
Fr. Robert: But we use it the same way as we used the sort function remember you just, you type the name of whatever’s going to be sorted and you type in sort. This is we typed anything that we want to become lower case and we use the function lower.
Shannon: So alternatively could I put upper and get upper case?
Dale: That's correct.
Fr. Robert: Bam.
Shannon: I'm smart.
Dale: And so I assign that back to rhymes and then I now open this – I'm opening the same file again, I just want to rewrite that file. So I'm opening the same file again but now instead of using r, I'm using w because I just want to write to it.
Fr. Robert: Uh-hmm.
Dale: And there are several modes that you can do. You can actually open a file and write appending lines to it.
Fr. Robert: Yeah, yeah.
Dale: So there are several uses that you can do but we're just going to focus on this for now. But just so you know that there's more that you can do.
Fr. Robert: Yeah reading and writing, the basic modes that you can use. Do that, master those before you move on to appending because appending gets really weird, especially if you don't know exactly what's going to the file, you end up with files that are totally screwed up. From experience, yes.
Dale: Just letting you know what's out there. But yeah we're sticking with this. An so I've got now rhymes file again, opened up. And I'm using .write, before we used .read, now we're using .write and as an argument I'm passing in my string file. This is what we want to write to the file. And then when that's complete, I close it again.
Shannon: Oh cool.
Dale: And then we print out, file converted to lower case—
Shannon: Just to let you know that's it’s done.
Fr. Robert: Yup.
Dale: We're not actually checking to see if it worked, there's no error, you know I'm pretty sure it worked. So I'm just going to pop over to the file itself, so you can see here it in all caps. Some rhymes I wrote.
Fr. Robert: It’s very angry rhyme.
Dale: Yeah. I guy likes to scream a lot. All right so now we’ll run this.
Fr. Robert: It’s processing.
Dale: File converted.
Fr. Robert: There you go, it pulled it in, it ran the function on it and it pushed it back out.
Dale: Yup, that fast. Yup that fast.
Fr. Robert: So now if I you go back.
Dale: That's right, if I go back now, they're all lower caser.
Fr. Robert: Now it’s like happy. No more angriness.
Shannon: That would be perfect for Youtube comments.
Fr. Robert: Now Dale let me ask you, hahaha, actually you could use the Youtube API to read all comments and put them all in lower case to make them less threatening.
Fr. Robert: This is, you know what actually, if someone does that, if someone actually does that, we will feature that on the next show.
Shannon: That's great.
Dale: Actually, the Youtube API does not officially support comments in V3 at this time.
Fr. Robert: But I've used it to get comments before, you just have to dig around a little bit. Again, advanced programming.
Dale: V2, yes not V3.
Fr. Robert: Now if there was a way to push it back up and replace all the upper case—
Shannon: That would be awesome.
Fr. Robert: No, that's different. Okay Dale, now we've shown them some very basic stuff here, what do you think would be a 101 appropriate homework example? So of course we want them to pull in from a file.
Shannon: I know what I'm going to do.
Fr. Robert: We know what she’s going to do. She’s going to be something on those reservations, the invitation things. But then we want them to do some sort of processing and push out. What would you say is the homework for our Coding 101 crew?
Dale: Let’s see, well we're still kind of on sorting, maybe they can sort something and pop it back out to a file?
Fr. Robert: That's a – oh you know what, how about this, remember how we showed numbers right?
Fr. Robert: So figure out a way to pull in numbers from an external file like I did, sort them and push them back out. I will give you a hint, if you just pull the numbers in with the code that we've given you, without changing anything, it’s not going to sort properly. You need something, I don't know maybe a function that we've played with before that makes sure that their actually integers that can be sorted. I'm just going to throw that out there.
Shannon: I know.
Fr. Robert: Stop.
Shannon: I want to figure out how to alphabetize them and then sort them like that.
Fr. Robert: There you go, actually there's a function for that. So here's the nice thing about this, this is what we really want to see in the next episode. Again, we gave you all these basic stuff but the functions that we've started to show you are just the tip of the iceberg. If you go anywhere, to a Python primer, you're going to find dozens, hundreds, thousands of different built-in functions that you can call right now to play with that data and then push it out. This is where it gets exciting folks.
Shannon: It does.
Fr. Robert: This is it. You've just been given the great circle of life from Python. You can now take data in, you can process it and push it out.
Shannon: Now I want to hold a lion up and be like “The code of life”.
Fr. Robert: [gibberish], I'm sorry that was totally wrong, we should never do that again. Dale thank you so very much. I'm going to put you on the spot here. What do you think we can do for next week, we've got two episodes left, I'm thinking—
Shannon: Just two, already.
Fr. Robert: I know right, yeah. We need to start doing something crazy. So if we we're going to get a little cray cray, what would we be doing?
Dale: Didn't we do that last week?
Fr. Robert: That's was minorly cray cray.
Dale: Okay, okay, all right.
Fr. Robert: I’ll let you think about it but where's your mind going?
Dale: Ooh, okay, hmm, maybe I don't know, do you still not want to talk about the installing new modules or—
Fr. Robert: Oh no, we have to do that. Yeah that's actually, that's very good because anyone who’s going to be programming for like Python – oh no, programming for the Raspberry pie, they're absolutely going to have to know how to do that.
Shannon: Good point.
Fr. Robert: Yeah bring in all the libraries. Dale Chase, Discovery Digital Networks. Bryan if you could drop a little bit of his music right now. You need to tell the folks where they can find you because again, you're not just a programmer, you don't just work for Discovery Digital, you are an artiste. We're hearing some of your work behind us, tell us all about it.
Dale: That's right, this is a track that's off of my latest full length TypeDef. I'm featuring King Pheenix on this one, but yeah you can find me at dchase.bandcamp.com. Yeah or you know dalechase.com but yeah.
Shannon: Oh don't mind us.
Fr. Robert: I'm sorry, no – Dale Chase it’s awesome it’s always a pleasure to have you on. Thank you so very much for sharing your code warrior knowledge and folks, we’ll see him next week.
Dale: Thanks for having me guys, talk to you later.
Fr. Robert: Now we know that we’ve kind of blown your mind a little bit. This was a lot of stuff okay.
Shannon: A little bit, yeah.
Fr. Robert: But again, a little bit of a review is always good. So we're going to see you xt week and hopefully you'll have played with pulling data in, sorting, doing something to it, pushing it out.
Shannon: This was exciting though, I loved this episode, yeah. I had a lot of fun with it.
Fr. Robert: I like the fact that we, you know we approached the end of this module we are really giving the folks a full circle view of Python right.
Shannon: Yes, I totally—
Fr. Robert: You actually can do something useful with what we've just given you.
Shannon: Sure this is all like – it’s all like the little beginner points but you can combine them all and make your own interesting really cool scripts so I can't wait to see what people show us.
Fr. Robert: Yeah, now if you have any trouble with what we've given you, don't forget you can always find detailed instructions, you can find detailed notes at our show page. Just go to twit.tv/coding and you're going to find this, which has all of our episodes along with the notes for each and every single project. And also a link to the Github that will include the Snubs compiled, include all of Snubs’ code, it will include my code and it will include Dale’s code so you can have a character for character copy of every piece of code that we've used on this week’s show.
Shannon: And of course don't forget we're also on iTunes. I'm so excited to be found on iTunes, apparently a lot of people have been subscribing over there and you should be one of them so that you could be an iCoder as well and make sure to spread the word with all your friends and family. Anybody that you think would be interested in Coding 101, we would really appreciate it.
Fr. Robert: Even if you don't think they'll be interested in Coding 101, if you think your code is going to be featured then yeah, you should probably be telling them.
Shannon: Oh yeah, definitely.
Fr. Robert: You can also find us on Youtube at Youtube.com/twitcoding101. Again, you'll find all our episodes there along with any other project that we start putting together. We that a lot of you don't watch us on Youtube but it’s always there as a possibility in case you need some coding goodness.
Shannon: Yes, go to our Google Plus page. That's our most exciting community. We have I think around a thousand people now.
Fr. Robert: Yeah we're getting up there. Whoa, what is that?
Fr. Robert: Oh, we're almost there.
Shannon: Almost there.
Fr. Robert: I'm saying by next week we're at a thousand.
Shannon: Look at all the people in there that are compiling code and sharing it with us. And you can be one of them. And if you want to share your code and ask around if you have any problems, everybody’s very friendly in there and very, very helpful. I've even had a few questions and they've helped me solve a few problems myself. You could go over to Plus.google.com/twitcoding101.
Fr. Robert: Right and you know we've said before the best way to become a programmer is to just poke around and if you want to poke around, the Google Plus community is a great way to make sure your poking in the right place.
Shannon: Check out the code. Best place.
Fr. Robert: Yeah, please do. Now if you're not into the Google Plus groove, you can also find us on that Tweeter, tweeter, tweeter thing.
Fr. Robert: You can find men @PadreSJ, that's twitter.com/PadreSJ.
Shannon: And I am twitter.com/Snubs.
Fr. Robert: Uh-hmm, now one last note, we actually do this show live every Thursday at 1:30 pm pacific time.
Shannon: I always forget about that.
Fr. Robert: I know right? And you get to see the pre-show, which actually goes on a long time, even the post-show and everything in between that maybe doesn't make it in the final cut. And as long as you're doing that, they could also find us at our chat room right?
Shannon: Yeah, what's our chat room? Irc.twit.tv.
Fr. Robert: Irc.twit.tv, oh and the channels on live.twit.tv. Jump in there, you'll see us actually talking to the chat room during the show. And who knows, maybe you'll get featured on Coding 101.
Fr. Robert: Yeah, yeah, until next time.
Shannon: I think that's about it for today.
Fr. Robert: Yeah I'm father Robert Ballecer.
Shannon: I'm Shannon Morse. End of line.
Fr. Robert: End of line.