Coding 101 17 (Transcript)
Shannon Morse: Today on Coding 101 we are reviewing outputting lists. We’re going deeper into sorting, and, it’s tuples time!
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 Hover.com. Offers the best way to buy and manage domain names. It’s simple, honest, and easy to use. For ten percent off your first purchase, go to hover.com and enter promo code C101. And, by Lynda.com. Learn what you want, when you want, with access to over 2,400 high-quality online courses and training videos, all for one low monthly price. To try it free for seven days, visit Lynda.com/c101. That’s L-Y-N-D-A dot com slash C one zero one.
Fr. Robert: Welcome to Coding 101. It’s the show here on TWiT where we let you into the world of the elite code monkey. I'm Father Robert Ballecer.
Shannon: And I’m Shannon Morse and for the next 30 minutes we are going to get you all coded up on everything you need to know to be a code warrior.
Fr. Robert: Shannon, it’s getting kind of sad, we’re getting close to the end of the Python module, here, right?
Shannon: No, don’t remind me, man.
Fr. Robert: I know.
Shannon: Don’t remind me.
Fr. Robert: It was a good idea at first. I remember, we’ll say we’ll do these eight-week modules. I’m getting into this language. I don’t want to go anywhere.
Shannon: I know. I started learning things and then I’m like, I don’t want to stop! Why do we gotta stop, man?
Fr. Robert: It’s true. It takes like four weeks just to get to the mode where you’re like starting to absorb the knowledge.
Fr. Robert: Speaking of absorbing the knowledge, we had some decent lessons last week.
Shannon: Oh, my gosh!
Fr. Robert: Did we get something for Snubs compiled?
Shannon: We did. We reviewed everything that we absolutely could, last week, and we also went through some sorting. We also did some sorts of alphabetical lists, and then we outputted that list to a file. So, I have my program this week—
Fr. Robert: Ooh.
Shannon: —right here. I’m gonna go ahead and open it for you. This is my wedding RSVP list, so it’s the same one that you saw a couple of weeks ago, basically has all the names, and then it outputs those into a file. But they aren’t in alphabetical order, so that’s not gonna help my venue very much—
Fr. Robert: Right, right.
Shannon: —so I hit Enter to continue and it alphabetizes the list. At the very end you’ll see it says “Guests are now sorted alphabetically in a new file. Press Enter to exit.” So I hit Exit and then I go down here and there’s a new file called Shannon Write and this has my—
Fr. Robert: Bam!
Shannon: —list of people alphabetized. Now, they aren’t separated right now. I know I need to add I think a splash N somewhere in there, which I’ll figure out, but it does have the alphabetized list, so it works!
Fr. Robert: So this—
Shannon: Dale helped me with this, thank you!
Fr. Robert: Exactly. This is what I like about this. So you’ve got the raw code, I mean, you’ve got the function working.
Fr. Robert: Now you can go back and say, you know what, I want this to look pretty.
Shannon: I can make it look pretty.
Fr. Robert: I want it to be comma-delineated.
Shannon: I can add a whole bunch more guest if I wanted to , because there’s no end to how many are in that range, so I can do as many as I want to if I want.
Fr. Robert: Exactly.
Shannon: It’s fun!
Fr. Robert: And once you’ve got that, it’s the circle of life in programming, once you have a way to pull data in from a disparate source, a text file—
Fr. Robert: —process it in some way, and then push it back out into, again, a text file, a disparate source I can push to something else, you really do have a complete program.
Shannon: You do.
Fr. Robert: It actually does something useful.
Shannon: Something useful, and that’s totally why I love doing this show. And I also wanted to show a couple of fan submission as well that we got on the Google Plus community. The first one was from Daniel, we showed his last week, so I’m not gonna show it again, but go over there and download his example. This is a perfect produce of what you can do by inputting information out of a couple of text files, and then outputting it into a very organized text file that you can actually use. Yay, usefulness! Now the other one I wanted to show is from Darrel. So if I go back over to my file directory, so this one’s from Darrel, he first has this interesting little memory database.
Fr. Robert: Whoa!
Shannon: Yeah, it’s so weird, it’s a text file. There’s a bunch of information in here, OK? Isn’t that crazy?
Fr. Robert: Well, obviously he has a bit of extra Python knowledge.
Shannon: All right, so I just made that into a text file so I could download it, and then I created a Python file. So if I click on the Python file, so basically what it’s taking me and doing—and this one does not output into a text file—this one just takes information from memory. So it has Padre, Shannon, Darrel, and Timmy, and it sorts them by name, by age, and by ZIP code. And I love how he organized it in this code so that each one is indented, so it’s a very nice, organized file of information and it kind gives you a nice little spreadsheet, almost. So if I hit Enter to close, I’m gonna go ahead and show you what his code looks like. So it’s a little bit advanced, but he has a lot of notes in here, so read the notes. It’s very helpful. But basically he has name.list.txt and then he has this list of people, their ages, and then their ZIP code, whatever that might be.
Fr. Robert: Mmm-hmm.
Shannon: I think he’s from Europe, given the four digits in the ZIP code. And then it outputs that information into the organized list for you, by age, by ZIP code, and by name.
Fr. Robert: Fantastic!
Fr. Robert: Now what I like about this, is, this is actually touching on something that’s we’re going to be talking about today, when we get a bit deeper into lists and when we get into tuples.
Shannon: Oh, boy!
Fr. Robert: Yeah, because remember the whole idea of having something like a list, and the reason why it’s more useful than, say, just a variable, like a variable test, is because it allows us to group together data, remember?
Shannon: Oh yeah, absolutely.
Fr. Robert: Super important, right?
Fr. Robert: Yeah. It’s one thing to have each person in your reception party have their own variable in order to be able to carry their name, and one to carry their age, and one to carry their table.
Shannon: Yeah, of course.
Fr. Robert: But when we can actually start grouping together useful information like name, and table, and age, or food preference, and we make that all one so we can—
Fr. Robert: Exactly.
Shannon: And then I could group them depending on who I’m sending the information to. If it’s for my photographer, I’ll sort them by who are family members, who I need to be photographed. If it’s to my venue, I could sort them by what table numbers and what food group that they decided to choose.
Fr. Robert: Yeah.
Shannon: All so fun.
Fr. Robert: Oh, by the way, we got Kyle Potts in the chat room who’s saying Notepad to program? Actually, yes, this is one of the reasons why we chose Python. The last language we did, C#, required a pretty complicated developer environment.
Fr. Robert: Python is nice because you can use Notepad. You can use V. You can use Emacs. Whatever you want.
Shannon: That’s why I’ve been doing so far, and I just add .py to the end and that’s how I save the file.
Fr. Robert: Yeah.
Shannon: It’s very easy for me to write into Notepad. The only issue that I’ve come across a couple of times is whenever I have an error, it doesn’t tell you where that error is.
Fr. Robert: Yeah.
Shannon: Because it closes too fast for you. It errors out.
Fr. Robert: Yeah.
Shannon: So you’ll have to go back and figure out where that error is yourself.
Fr. Robert: And the other thing about using just any sort of text editors, remember that Python is extremely white-space sensitive.
Fr. Robert: It’s not like C#, where as long as you end the line with the proper comma delineator, it’ll end the line and move on to the next, Python looks at where things are indented, it looks at where things are placed in the page, so if you do use a text editor that’s not Notepad or one of the standards, just make sure that’s it’s not adding extra control characters or spaces.
Shannon: Yeah. Absolutely. I ran into that problem too.
Fr. Robert: Which is good, right?
Shannon: I should show you my code for my example, though.
Fr. Robert: Yes, please.
Shannon: Let me run over here, back to my folder, because I did have a question for our audience. I wanted to find out what they thought of my code. There we go, slash idle, all right. So here’s my code for my example that I decided to show off. This is the wedding RSVP list. First I opened and read the file, and then I sorted them right here, and this is just a simple sort list, and at the very end I outputted that sorted list into a file. Now I know that I have to add that slide, and I think it goes right here, but I’m pretty sure maybe I need quotes here. I’ll have to look that up, I’ll have to research that.
Fr. Robert: We’ll fix it. We’ll make it right.
Shannon: So I want to ask you guys to fix that for me.
Fr. Robert: That’s a good homework. This’ll be in the Github, so go in there and fix Shannon’s code.
Shannon: Yes, it will be.
Fr. Robert: And also make it pretty.
Shannon: Yeah, make it pretty.
Fr. Robert: Menus and everything.
Shannon: I want to make it pretty.
Fr. Robert: Go ASCII crazy.
Shannon: Menus, really? ASCII?
Fr. Robert: Why not? They can do it.
Shannon: Do it.
Fr. Robert: They’re smart.
Shannon: Have fun.
Fr. Robert: Now, when we come back, we’re going to be getting a little deep into some of those data thingies, structures—
Shannon: Like tuples?
Fr. Robert: —lists and tuples that we were talking about.
Fr. Robert: We’re gonna get into Tupperware, tuple-ware, sorry. I always mess that up.
Shannon: Welcome to the tuple-ware party!
Fr. Robert: Exactly. It keeps your data fresh.
Fr. Robert: No.
Fr. Robert: No.
Shannon: No. We hear a wa-wa from the audience.
Fr. Robert: Sad trombone. But you know what’s not a sad trombone?
Fr. Robert: Hover.
Fr. Robert: Yeah, so one of the sponsors—
[sad trombone sound effect]
Fr. Robert: Hover does not get sad trombone. What’s wrong with you, Bryan?
Bryan: I’m totally on top of this.
Fr. Robert: You have way too many buttons. But seriously, we here are all about the interwebs, right?
Fr. Robert: We like ourselves some interwebs.
Shannon: I love the interwebs.
Fr. Robert: And in order to have a presence—
Shannon: Interwebs citizen.
Fr. Robert: Yeah, exactly. We’re both citizens of the interwebs.
Fr. Robert: But you know what you need, like your passport, to be traveling through the interwebs?
Shannon: Oh, what’s that?
Fr. Robert: A domain.
Shannon: Oh yeah, you need a domain.
Fr. Robert: Yeah. Now there are a lot of fly-by-night outfits. You know them, I’m not gonna have to mention them. I don’t want to mention them. But you really want someone who you can trust, before you trust them with your domain. I mean this is your identity online. You want to make sure it’s done right. Which is why were’ so happy to have Hover on Coding 101. Now, you want to secure that domain. You want something catchy and memorable to represent your online identity. Well, Hover gives you exactly what you need to get that job done. You’ll find the perfect domain for your ideas so you can get started working on it, and move on to the next thing on your to-do list. In other words, don’t spend all your time figuring out how you’re gonna get that domain you want, just go to Hover, buy it, be done, and start creating. Now people love Hover. When you’re looking for the best place to register a domain name, you ask the people who know a lot about domains. Like Stubs and myself. Geeks, developers, designers, and programmers all love Hover because they know that they’ll have the best tools and support. But you don’t have to be an expert to get a domain. Their service is simple enough to use that you’ll be comfortable figuring it out yourself, and the support team is always ready to give you a hand. Now Hover takes all the hassle and friction out of registering a domain. It gives you an easy-to-use, powerful tool to use to manage your domain so that you can do it, well just in your pajamas. You can get the perfect domain name and start building your web presence right away to take control of what people find when they search for you or your online business. Now, if you wanted to use Hover, it’s easy. All you have to do is search for a few key words and Hover will show you the best available options and suggestions. Hover has a clean and simple web site. In less than five minutes you can find the domain name you want and get it up and running. And they got a huge variety of domain extensions , like .com, .net, and country codes that best suit your needs, so you can get just about anything. Now, here’s the big thing for me. Hover is honest. They don’t believe in heavy-handed upselling. They’re not gonna make you get this package or that package when you buy your domain. They tell you what it’s gonna cost and that’s exactly how much it’s going to cost. They include everything that you need with your domain, no more, and no less. Plus, WHOIS privacy is included free on every domain that supports it. Now if you ever need a hand, Hover is, well, just a phone call and an email away. They’ve got the best customer support, hands down. They’re for its no-wait, no-hold, no-transfer phone service, and when you call you actually get a real live person. How about that? Now what got me excited is that Hover actually has a lot of the new key TLDs, including the .club. Now you don’t have to settle for something that’s less than perfect anymore by adding a dash or dropping a letter. For anyone who’s tried to register a domain nowadays knows how difficult it is to get something in the .com, .net, or .org space. You always have to find a freaky spelling. Well don’t do that! Instead, get a .club. It’s perfect for any kind of club or social group. A country club, a book club, a sports club, even Facebook groups. There are so many applications for .club, and they’re descriptive. .Club has its meaning built into it. Which means people will know what your web site is about. Your domain can be short by eliminating that club from the actual domain name, and they’ve got a lot of them available. You have a better chance of getting your name, so you can get rid of all those dashes or words you didn’t want there in the first place. So here’s what we want you to do. We want you to visit hover.com to register your domain name, and get 10% off your first purchase. All you have to do is go to hover.com and use our promo code: C101. That’s hover.com and our promo code C one zero one. And we thank Hover for their support of Coding 101.
Fr. Robert: Woot! Yah-tah!
Fr. Robert: Now—
Shannon: Let’s do some more now.
Fr. Robert: Let’s do some more. So, we gotta talk a little something-something before we can get into tuples.
Shannon: A little something-something?
Fr. Robert: A little something-something.
Fr. Robert: There are actually a few things that I think we should probably review before we get into tuples. It’s always good to go back over some of the material that we’ve covered, starting with, well, a question from Ted Bigham, who asks, why do certain chipsets support certain languages? This was actually something I found on Twitter. This is actually a good question, right?
Shannon: That’s a really good question.
Fr. Robert: Right, you always hear about, oh, well, use Python if you’re programming for the Raspberry Pi, and C#’s really good for Windows on an X86. And he wanted to know, basically, well, do you have to use C# if you’re on an X86 processor, do you have to use Python if you’re—
Shannon: Not necessarily.
Fr. Robert: Yeah, here’s the thing: It’s not bound by the processor. The way that compilers or interpreters work, like the one allows C# to work or the one that allows to Python to work, is it takes that language, everything that we write, and it converts in into machine code. Normally, binaries or an executable.
Shannon: The ones and zeroes.
Fr. Robert: The ones and zeroes that the processor can actually understand.
Fr. Robert: Or at least the operating system can understand. So when we talk about a compiler or an IDE or an interpreter being tied a specific processor, it’s not because that processor speaks Python, or that processor speaks C#. It’s because the compiler has been designed so it will convert the language into the machine code for that specific processor.
Fr. Robert: Does that make sense?
Shannon: Yeah, it does. That’s interesting.
Fr. Robert: We get a lot of people who are confused about that, because they think that, you know, when I type in Print in Python that it means the processor actually understands what Print is. It has no clue. The interpreter for Python actually has to turn that into something that the OS can read and then can push to the processor, the machine level.
Shannon: That would make sense, given the name of these things is compilers.
Fr. Robert: Yeah, it’s a little bit of basic knowledge and Ted, thank you so very much for asking, because I think that’s something that a lot of people were wondering about. Theoretically if you wanted to, you could take something like C# and you could recompile the compiler so that it would work on every type of processor. We just typically don’t do that, because that’s boring.
Shannon: We won’t do that on this show.
Fr. Robert: We won’t do that on this show. All right, now let’s get to a little something-something.
Fr. Robert: We know about lists. We’ve been playing a lot with lists.
Shannon: Yes. I love lists.
Fr. Robert: Well, we have a way of printing from lists, right? We’ve been using this. If you go ahead and go to my computer, this is the method that we’ve been using to print. So we’ve got this list called podcasters that’s filled with Leo, Lisa, Padre, Snubs, Bryan, OMGchad, and then 1234. That’s our newest podcaster.
Shannon: 1234, yeah, he’s great.
Fr. Robert: He’s awesome, he’s gonna take my place. And then we’ve been using a “for loop,” right? We go through this list and we say print the list and then the entry number in the list, and as it goes from zero to seven, it will go through all the different entries within that list, and you get this: Boom! It prints up everything that’s in the list. Right?
Shannon: Yes, exactly, from the very first one all the way to the end.
Fr. Robert: All the way to the end. So this is what we’ve been using when we’ve been wanting to print from a list. This works, and this is good, and actually I like doing it this way, because it does make you use for loops. But there’s actually an easier way that I want to show you right now, and that’s to do this. Remember that a list really just looks like a variable to Python. So I have the same thing; I have my list with all those people, and instead of using a for loop I’m just gonna say print podcasters. I’m just gonna say print the list. Now, you think you know what this is gonna look like?
Shannon: Will it look exactly the same?
Fr. Robert: Almost. It’ll look like this: So when I print this…
Shannon: Oh, one line.
Fr. Robert: Yes, it’s got them all in one line. So basically all it did was it did a dump of everything that was in the list.
Fr. Robert: Right? So it will give you the same results, and you’ll see what’s inside the list, but the format is slightly different.
Shannon: Yeah, it even includes the brackets and the little quotes.
Fr. Robert: Right, right. It’s literally printing everything that’s in that list.
Shannon: Because it doesn’t know that that’s actually telling you that it’s a range.
Fr. Robert: Right, it just dumping. It’s dumping everything. So the list is a memory address. Saying print the list actually just dumps that memory address on the screen. Whereas if we use the for loop, we’re pulling each individual element out of each piece of that list and printing it to screen.
Shannon: Got it. That’s cool.
Fr. Robert: It’s kind of cool, right?
Shannon: Yeah. It looks a little easier, but it’s not as pretty.
Fr. Robert: Exactly. It’s not as pretty, and also, remember, the reason why we wanted you to do it with a for loop is because you could add things to that for loop to play with the elements, right?
Fr. Robert: If I’m just printing out the memory dump of the list, there’s not a whole lot I can do. I shows me what’s in there, but beyond that not really useful.
Shannon: Right, yeah.
Fr. Robert: Yeah, yeah. So that’s the second way to do print. What we also wanted to do is we wanted to show you the way that we’ve been doing sorts. So this is our sorts, right? We’ve got a list called SortMe, it’s filled with all these numbers. We’ve seen this example before. And then I’m using a for loop to go ahead and print what’s in that list, and then I run the .sort function. Remember this?
Shannon: Yes, I do.
Fr. Robert: So all we have to do is we do sort and we add that function, we append it to the name of the list, and it will do a destructive. It’s called an in-place sort. So it’s going to reorder the list called SortMe with the reordered list.
Shannon: Oh, that’s weird.
Fr. Robert: Here’s what it looks like. The first time it runs, it’s gonna print what was inside that list, what I gave it: 1, 23, 6, 4, 99. Right? So when I press Enter, it now shows me, Oh! It’s all been sorted: 1, 4, 5, 23, 99.
Fr. Robert: But here’s the thing: What I’ve just done, is I’ve overwritten the list called SortMe. It’s been destroyed. It’s been destroyed in favor of the list that is sorted.
Fr. Robert: It’s a destructive sort.
Shannon: That’s so weird!
Fr. Robert: Right, right. That’s good. Sometimes it’s good. If you don’t care what’s going on with the original array, then that’s great. But, we can do a non-destructive sort.
Shannon: Oh, lord, what is going on here?
Fr. Robert: All right, so, we’ve got the same thing, we’ve got a list here called podcasters, filled with all those podcasters—
Shannon: And then you just print that—
Fr. Robert: Right. Just gonna print it, to show you what’s in there.
Fr. Robert: Right? Now I’m gonna sort it, but I’m gonna sort it two different ways. So, I’m gonna use not sort, but I’m gonna use the function called sorted, and what I’m gonna do is I’m gonna say this new list, called sortedpodcasters, will be equal to—so it’s going to be filled with the value—of the list called podcasters, but sorted.
Fr. Robert: OK. The reason why we’re doing this is it’s going to keep the original list while also giving me a sorted list.
Shannon: Ah, OK, so it’s not destroying it in the process.
Fr. Robert: Right, this is not an in-place sort, it’s not a destructive sort; it’s a sort that gets pushed into a new list. So when I run this, I get this. That’s just a dump, right?
Fr. Robert: OK, so, go ahead and hit Enter. Now I get two: I get the original list, which matched up to what you see at the top, but I also get the sorted list, which is now sorted alphabetically, right?
Shannon: That’s cool!
Fr. Robert: Now the cool thing about this is I now have two copies of that list, so if I ever wanted to go back and compare lists, I could say I’ve got the pre-sort and the post-sort list.
Shannon: So I wanna run through the rest of your code for this, so I can see the differences.
Fr. Robert: All right. I gotta look over here. There we go, OK.
Shannon: OK, so first you have print podcasters—
Fr. Robert: Right.
Shannon: —and then you have print sortedpodcasters. Oh, yeah, that’s easy!
Fr. Robert: Very easy, right?
Shannon: OK. So the first time it prints the podcasters, the original list, and then second time it prints sortedpodcasters, the new variable that you made.
Fr. Robert: And this is the key line right here. So the key line is sortedpodcasters is equal to sorted(podcasters), right?
Shannon: Perfect. OK.
Fr. Robert: So what I’ve done is, I’m not changing the original list. The original list, called podcasters, stays the same. But I’ve run the function called sorted on it, which doesn’t destroy the original list, it just pushes those values to a new list called sortedpodcasters.
Shannon: That’s awesome!
Fr. Robert: Yeah. It’s actually another way that you can manipulate data. It’s very useful.
Shannon: Very easy.
Fr. Robert: All right, so, now, let’s do a little something-something. In the previous example, what we did was we pushed the sorted list into a new list, right?
Fr. Robert: Well there are going to be some cases where you don’t want a brand new list. You just want it sorted at that moment and then you don’t care about the sorted values anymore.
Fr. Robert: In Python, you can actually do this: I can use the sorted function and I can print it.
Fr. Robert: So what this does is—again, I’m always using the same list here, people, the same list called podcasters—now I’m going to print that list sorted.
Shannon: Print podcasters sorted. OK, cool.
Fr. Robert: What this will give me is this. So—
Shannon: Got it!
Fr. Robert: Right?
Shannon: That’s exactly what I thought it would look like.
Fr. Robert: Exactly. Now the cool thing about that is, I didn’t have to create another variable, I don’t have to have another piece of memory that’s being used up by a list. This is just printed. It just prints it, and after it’s done printing, it goes away.
Shannon: That’s cool. Nice and easy.
Fr. Robert: Nice and easy. Now let’s to get to something that maybe people won’t feel is so easy. We’re talking about tuples.
Shannon: OK, tuples.
Fr. Robert: OK.
Shannon: So this is a real thing, you’re not just making it up?
Fr. Robert: I’m not making this up. This is— I do wanna know who came up with the name, tuple.
Fr. Robert: It’s kinda cool. I love it.
Shannon: It’s like quintuples.
Fr. Robert: Tuple. I like tupleware. Tupleware is my big fan.
Fr. Robert: So if you look at the screen, what we have here is what a tuple looks like. A tuple almost looks like a list.
Fr. Robert: There’s one subtle thing. Instead of a bracket, I’m using a parentheses.
Fr. Robert: Right?
Fr. Robert: OK. Here’s the other thing about a tuple. These are indelible. You cannot change them.
Shannon: Oh, OK.
Fr. Robert: A tuple is permanent. I create it. Once you create that tuple, unless you wanna destroy the record, you can’t change what’s in it.
Fr. Robert: Which has its own uses, right? So you’ve got semi-permanent information. In this particular example, I’ve created three different tuples. So again, remember, every kind of variable, be it a list, an array, or just a straight-up string, or an integer, looks the same in Python. In this particular case, I’m creating a tuple called tuppy1—
Fr. Robert: —and I’m filling with those six numbers.
Fr. Robert: In the second instance, I’m creating tuppy2, and I’m filling it with these strings. In the third case, I’m creating one called tuppy3, and notice how I didn’t use parentheses. Instead, I used a semicolon at the end, right? Now, when I print this, it—exactly like you think—it’s just gonna print exactly what was in those tuples.
Fr. Robert: Easy, right?
Shannon: Yeah. Looks easy enough.
Fr. Robert: Easy enough. OK, good. So that’s basic tuples, and again, the reason we want to use this is anytime we have groups of information that we want to stay together, that we don’t want to change.
Fr. Robert: All right? We want to make them permanent. All right, so now let’s go ahead and move from that to this.
Fr. Robert: So, we know about lists.
Shannon: Yes, we do.
Fr. Robert: We know about tuples.
Fr. Robert: Now how about a list of tuples?
Shannon: Oh, boy.
Fr. Robert: This is actually perfect for you, because when you start talking about your reservations, what are you gonna have? You’re gonna have name, you’re gonna have age, you’re gonna have all the— Well this is like a list of lists. But this is a list of tuples. Go ahead and go back to my screen, Bryan, and let me show you what I’m talking about. I’m creating a list called quintuples.
Fr. Robert: I’m gonna fill it with three tuples.
Fr. Robert: The first one is Padre, my name, TWiET, which is the name of the show I host, and 72, which is my age. And then it’s filled with you Snubs, and you host Coding 101, filled with your age, 22.
Shannon: Right, yeah.
Fr. Robert: And then it’s filled with Bryan, he hosts KnowHow, and his age is 6.
Fr. Robert: Right? Exactly. So what I’ve done is I’ve created the list that actually contains three tuples—
Fr. Robert: —and the reason why I do this is to show you, just we like we started at the beginning of the lesson, it’s really easy to print what’s in here. All I have to do is say print the name of the list, and list is twit_tuples. And if I go ahead and run this, it’s just gonna print it out.
Fr. Robert: Right?
Fr. Robert: OK. So far, interesting, not useful.
Shannon: Not useful at all, really.
Fr. Robert: Here’s where it gets useful. So—I’ve got so many shells open right now—what I’m gonna do is I’m gonna sort it.
Fr. Robert: So same thing. I’ve got, all this is just what you saw. It’s going to create a list filled with tuples, it’s going to create the tuples with name, show, and age. But then it’s gonna do this. Remember the sorted function we just played with?
Shannon: Yeah, yeah.
Fr. Robert: It’s gonna say print the sorted version—
Shannon: Of twit_tuples.
Fr. Robert: —of twit_tuples.
Shannon: Oh, I’ve seen this! Key lambda!
Fr. Robert: Key—
Shannon: What is that?
Fr. Robert: This is where it gets useful. What we are now doing is we can direct, using a key, how it sorts.
Shannon: Oh, that’s cool. So the user can input something—
Fr. Robert: Well, no. Right here, I get to tell it how it’s gonna sort. So I’m saying you’re gonna use the lambda key. And actually it’s very easy if you just go to the Python documentations and you can see all of the keys that you can possibly use. Go back to my code. It’s gonna use the lambda key against twit, and what that’s saying is, go to the third field. Because remember it starts at zero. So 0, 1, 2—
Shannon: 72, 22, and the 6.
Fr. Robert: Right. And it’s gonna say sort by the third field.
Fr. Robert: Which is age, so what it’s gonna do, it’s gonna sort by age.
Shannon: That’s cool!
Fr. Robert: So if I run this, now it’s reordered everything so that it sorts it by order of age, in descending order.
Fr. Robert: Yeah. And I can use that key on the second field, I can use it on the first field. This is where sorts really become useful, because once I’ve got a collection of data, I can say sort it by name. Now sort it by show. Now sort it by age. Now sort it by ascending or descending order. And that’s all in the key.
Shannon: Wow, I can totally see how this would be really, really useful for my list of people that are coming to the wedding.
Fr. Robert: Exactly. Because now it extends what you can do. It’s not just, well, I sorted, that’s the only thing I can do. Now, sort it up, sort it down, sort it by name, sort it by age, sort it by the table you’ve put them on. As long as you’ve put everything in the right list.
Shannon: I’m so glad you went over landis, because I was like, what the— Like, why do you use that in code? I don’t understand.
Fr. Robert: Yeah, yeah. Well, this is all useful information, but you know one of the things that we really wanted to do when we come back, is we wanted to talk to our code warrior about how we get other packages in the Python. We’ve taken you though some of the basics, but what would be awesome is if he’ll show us how we can take pre-written packages and use them to extend our programming masterstroke.
Fr. Robert: But before that, you know what?
Shannon: I think there’s something that I want to mention.
Fr. Robert: Yeah, yeah. What I’m feeling, I’m feeling like this is all good, but I would love to learn more.
Shannon: You want to learn some more?
Fr. Robert: Yeah. We give people little bit and pieces, but—
Shannon: We do.
Fr. Robert: —but let’s say I actually wanted to learn Python from the beginning, the way it’s designed to be taught.
Shannon: I know the exact spot that you can go to.
Fr. Robert: Where?
Fr. Robert: Nice.
Shannon: Yeah. They’ve got over 2,400 courses, and they’re adding new ones every single week. From people that are passionate in their fields of service. They do videos in everything from Python to Java, to beginner programming, to IOS programming. Everything that you could ever want, and ever need on that website is there. And there are courses for everybody from beginners to intermediate to really, really advanced stuff, in case you feel like, you know, I already know the beginner stuff, I can skip this. And you can watch from your computer or your tablet or your mobile device, in case you’re on the go all the time, like I am. I’m commuting every single day. So I watch this stuff. Well I listen to it—excuse me—while I’m driving. I listen to it on my way to work. Very, very easy, and very fun. And these people really like to do these videos. So you’re not gonna watch, you know, it’s not gonna be something that is home-made on Youtube, like you would find sometimes. Lynda.com helps you keep up to date with software, learn brand new skills, and you can explore new hobbies with easy-to-follow video tutorials. So whether you want to master the fundamentals of programming or learn a new programming language, like Python, or build your first IOS app, Lynda.com offers thousands of courses on a variety of topics, so you can learn how to program software and applications, with hundreds of courses on widely used programming languages, like PHP, Objective-C, and Java. At Lynda.com, you can learn hundreds of courses on web design and development skill, everything from the web fundamentals and mobile development to responsive web design and user experience. Interested in creating an app for multiple platforms? Well Lynda.com is the place to go. They created a playlist for building a cross-platform mobile app that is available now at L-Y-N-D-A dot com slash C one zero one. Yay! And Lynda.com just released a course on getting up and running on asp.net, so you can explore asp.net, including the structure behind robust asp.net applications and the tools that you need to manage data, construct APIs, and establish real-time web connections. You gotta check this out. Lynda.com works with software companies to provide you updated training the same day new versions hit the market, so you always have the very latest skills. And you can add certifications to your LinkedIn profile, which is supercool if you're looking for a new job and you need to have those kind of certifications for that said job. So, it’s only 25 bucks a month for access to the entire Lynda.com course library. Or for 37.50 a month you can subscribe to the premium plan—that’s what I was doing for a long time—which includes exercise files that let you follow along with the instructor’s project using the exact same project assets that they do. Extremely helpful, especially if you’re more of a homework person, like I am. So you can download everything you need right there. And you can try Lynda.com right free for seven days. For a free trial you can visit Lynda.com/c101 to access the entire library. That's over 2,400 courses, free, for seven days! Do it now! That's L-Y-N-D-A dot com slash C one zero one. And we thank Lynda.com for their support of Coding 101. Thank you, guys. We love you.
Fr. Robert: Do it now!
Shannon: Do it now!
Fr. Robert: Now! Do it now! OK. Now, we also have people in the chatroom who are fighting over the pronunciation of tuple. It’s like gif and “jif.” Find the pronunciation that you like, and go with it.
Shannon: Yep, that’s right.
Fr. Robert: Now, Shannon, this is the time of the show when we bring in someone who actually knows what he’s doing, right?
Shannon: I think we do. Who could it be?
Fr. Robert: Who could it be? Ladies and gentlemen, welcome back to the show our code warrior, the man who knows, Mr. Dale Chase from Discovery Digital Networks. Dale, so good to see you again. How are you?
Shannon: What’s up, Dale?
Dale Chase: How you doing, guys?
Shannon: Doing awesome!
Dale: So I’m the one who knows what he’s doing?
Fr. Robert: Yes! That’s how it works.
Shannon: You do! You always know what you’re doing.
Fr. Robert: Oh, by the way, so we gotta ask you, how do you properly pronounce tuple?
Dale: How do you properly pronounce—? I pronounce it “too-ple.”
Shannon: Toople. Interesting.
Fr. Robert: You know what? You can’t say toopleware.
Shannon: I think it’s one of those things were it depends on where you grew up and the way that you grew up.
Fr. Robert: I like “tup-le.”
Shannon: Tuple, gif—
Fr. Robert: Tuple sounds cute. Toople sounds pretentious.
Dale: Toople is how they pronounce it in Brooklyn.
Fr. Robert: And if you mispronounce it in Brooklyn, they will break your knees. Now Dale, everything that we’ve been doing in this module, seven weeks of this module, has been based on the programming that you have been doing. You’ve been giving us the master code every single week. It’s what we teach to, it’s what we show the people, it’s the example that we give to every code monkey who wants to get on his way—his or her way, sorry—
Shannon: Yes, that’s right.
Fr. Robert: —to code warrior stardom. But you wanted to do something a little different today. It’s not really a coding project, it’s more of a knowledge project. You want to hit us with it?
Dale: Yeah, yeah, yeah. Because a lot of the fun that I had learning how to code was, like, grabbing packages that added functionality to the language that I was working in, to help me have more fun with it. And something that I had a lot of fun with early on was coding my own games. So I thought it might be fun to show folks how to install a third-party package that would allow them to have, like, 2-D, 3-D graphics functionality.
Shannon: Oh, my God.
Dale: And to mess around with that.
Fr. Robert: No, not interested in that at all.
Shannon: Are you kidding me? Padre! This is awesome.
Fr. Robert: Just break it down for a second. Because some people may not understand exactly how significant that was. Most of us, if we think about programming a video game, it’s a freaking nightmare, because I don’t want to have to create all the code to make sprites—
Shannon: It sounds scary.
Fr. Robert: —and collision detection.
Shannon: But he said you can download that.
Fr. Robert: Exactly. What is this? Why would I be able to download this?
Dale: Yeah. I actually haven’t had much experience with this particular package, I just thought it looked pretty cool. I’ve found, actually, a video on Youtube of someone actually creating a Minecraft clone using this library called pyglet.
Fr. Robert: You mean we could get away from Java?
Dale: Yeah. Well, I mean, you know, it’s Python, so it’s like, it’s multiplatform but, you know, certain things you have to consider, but I figured it would be a nice fun way to kind of continue to learn Python within your own sort of environment.
Fr. Robert: Right.
Dale: Probably not being able to do much more with it, you know, without going through a fashion, you know, but nice to help you learn to continue programming, I think.
Fr. Robert: Dale, we’ve already talked about using functions that we haven’t written, right? I mean, just bring them into your program so you can call on them. Or even use them to build in functions in Python. We’ve also talked about using the API from something like Youtube, so you could use programming across the web. You could query them with queries and they bring you back information, no matter what you’re doing as long as you’re following the API standards. How is this different? I mean, this is not another function, this is not an API, this is an entire package that you bring into the programming environment.
Dale: Yeah, this is pre-written source code, so that, you know, it makes creating shapes and that sort of thing, and color, and streams and whatnot, just super easy, or easier than doing it from a much lower level. This kind of gives you, this is pre-written functions essentially, that gives you more functionality in Python.
Fr. Robert: OK. I’m sold. So I love the fact that I can do this. Can you actually show me what I would have to do to bring a package into my environment?
Dale: OK, well, in this instance—and generally it’s pretty simple—in this instance we’re just going to go to pyglet, p-y-g-l-e-t.org, and go to their downloads page, and since we’re using Python 2.7, we are going to use actually their alpha build here, from their latest repository version, because we need it to run in 32-bit mode, and not 64. Or is it the other way around? No, yeah, this runs in 64-bit mode, not 32. So I’m going to grab, here we are here, this 1.2 alpha one here, and just download this.
Fr. Robert: You don’t have any reservations about using the alpha code?
Dale: Generally, yeah, if this is not going to be something that we’re going to, you know, for sale, kind of just something we’re going to have some fun with, and generally I go with a stabler version, but since their stable version only runs in 32-bit mode for Python 2.7, I’m going to go with this just for the purpose of this demonstration.
Fr. Robert: Got it, got it.
Fr. Robert: You downloaded it and—
Dale: Yeah, so now I’ve got that downloaded, that’s in my downloads folder, so we’ll just go, and yes I am doing this live, so I’m totally on the wild side right now.
Shannon: Oh, snap!
Dale: So we’ll go to my downloads here, and that was—what was that file?
Fr. Robert: Pyglet-1.2.alpha1.tar…
Dale: So the first thing we have to do is, this is a compressed file, so the first thing we have to do is unzip it, or uncompress it, and I’m going to use a build-in command tar, and it takes a couple of arguments—
Shannon: We’re doing some command-line stuff.
Dale: So this will just uncompress it and put it right back in the folder where I just— Um… There we go. And so that just—
Fr. Robert: So these are all the Python files that were inside that package? They’ve just uncompressed.
Dale: Uncompressed and kind of—
Shannon: For anyone that’s used the tar.gz, it’s pretty much like a zip, like a zip file.
Fr. Robert: Yeah, you just find the right utility.
Dale: Exactly. So, installing this is now is actually is relatively simple, if all goes as it should. All I do now is go into that directory, that folder that was just created, and the command that we want to run—on a UNIX system, you’ll probably want to use another command, called sudo, to make sure you are—
Fr. Robert: Administrator.
Dale: Exactly. Running as an administrator to make sure it has all right privileges, and so we’ll just go, sudo python setup.py, and then you wanna run the command install. You can also add to this if you’re concerned about uninstalling it later. Actually I meant to have this ready, but there’s another thing you can append here, called record, and then some flags for that, that’ll let you put a text file in this folder, which keeps a record of all the files that it’s putting in, and you can just reference that when you want to uninstall it. But you can easily find information on that, if you want to go that far. But simply just the bare-bones, is just do “sudo python setup.py install” and I’ll put in my password.
Shannon: His password is ilovecats.
Fr. Robert: 1234.
Dale: Close. And now it’s installed.
Fr. Robert: And if you’ve ever played with a Linux system, or if you’ve ever played a Raspberry Pi, you know what this is.
Shannon: Yeah. You know how to do this.
Fr. Robert: It’s compiling your library, and it’s giving you something you can actually use.
Dale: Right. And I can prove this—and hopefully I can prove this—so now if I go into actually my Python interpreter, or the thing here, I just do, so we have our import command that you’d see us use—
Fr. Robert: Right.
Dale: —in a few episodes. So now I can do it live here, right, so I can say import pyglet, and if it’s in here, I will not get an error.
Fr. Robert: Cross fingers... Boom!
Shannon: It worked!
Fr. Robert: Yay! Take that, Murphy!
Dale: So now I can go ahead and start using the library.
Fr. Robert: OK, so yeah, so this, thank you for showing this, because there are going to be some people who freak out when they download this package, and they’re not going to know how to use it.
Shannon: Like me!
Fr. Robert: Right. But now we know, we know how to unzip it, we know how to compile it, we know how to make it ready for use. But how do I go from doing that to being able to program my own game? Can you show me how I use this library?
Dale: Yeah. I can show you, I can grab a couple of the example files that they have, and run them, and show you kind of how simple it is, to kind of just get in there.
Fr. Robert: Do that.
Dale: All right. So if we go back to their site, they do have some documentation here.
Fr. Robert: You’re gonna need the documentation.
Dale: Yeah, so within that they have some example files, and some things I guess to see if, you know, it’ll work correctly. Some tests that it goes through.
Fr. Robert: Right, right.
Dale: So, we’ll go ahead— I’m just gonna go ahead and unzip that normally.
Fr. Robert: Yeah, folks, and while Dale does this, this just shows you that if you ever make a library for someone else to use, please document the heck out of it.
Shannon: Yes, absolutely. There’s been numerous occasions where I’ve tried to do some kind of hack or use open-source software where there was no documentation.
Fr. Robert: Yeah, it’s like “Well I know how to use it, you should be able to figure it out.” It’s like, no. Assume people are stupid, and document everything.
Dale: All right, so we’re gonna go into the docs here, let’s see…
Dale: —so there’s a, I think that’s a folder—
Shannon: Read me!
Fr. Robert: Yeah, yeah.
Dale: I’m gonna open that up. OK, so, here we go, test.py in here. So I’m gonna run this test up high, python test up high, and cross our fingers again, and see what we get.
Dale: All right. So it’s running some tests here. And says, OK, so what’s going to happen now, it’s gonna create a small window that will be opened, colored purple, and that’s the beginning of the test. It looks like it opened up behind, but there it is.
Fr. Robert: Hi, test! So all that it’s doing is it’s showing it can actually use your graphic subsystem.
Dale: Yeah, yeah. And so now, if you wanna take a look, and it’s gonna go on, you know, just doing some tests. A couple of these are kind of interesting. I guess this creates two windows, I think. Or just another one. So this is just gonna go on, but what we can do is just look at the code.
Fr. Robert: Yeah, let’s check it out, see what exactly was going on in there.
Dale: Yeah. Yeah. And it looks like it’s got a bunch of dependencies in here.
Fr. Robert: Whoa!
Dale: Yeah. So this is all—
Shannon: Oh, that looks easy!
Dale: Well, yeah, this is just all the welcome and text. This is all the use stuff, it looks like.
Fr. Robert: Right.
Shannon: It’s the man page.
Dale: Yeah, so here we go. Here we get to some stuff here.
Fr. Robert: Yeah, so it’s importing the files from that library that it needs, and then where does it actually start using it? So let me break that down for you. So let’s say one of our code monkeys does all these steps and gets it properly installed, has the libraries ready to use, and says I wanna create a box, a graphical box on my screen, how do I use this library to do it? Where would I find that code?
Dale: Well all right, let’s look at the documentation, because that’s really where all this is. I mean, that’s, let’s see, let’s look specifically at something very specific, one of these other things here. Graphics.
Fr. Robert: Yeah. OK, so what it’s doing is it’s defining it, it’s using the define function to define certain function that you can use once you’ve got this library imported. I like that.
Dale: Well I think, yeah, I think these are just individual examples now, I think the test that I ran was kind of an overall, you know, test your system kind of thing. Now these are more specific examples that now we kind of look at exactly how this library’s used. So, yeah, so this is kind of bringing in math, some things from math, and then, so here we go, you’re importing pyglet, and then, I guess from this other sub-thing, all of it, and I guess this is all kind of set-up stuff.
Fr. Robert: Right.
Dale: Set up window.
Fr. Robert: Right. Right.
Dale: So now in here, that’s more window.
Fr. Robert: Well the important thing here is to remember that, again, this is looking crazy, right? This is gonna kill some people.
Shannon: Yeah. Don’t expect me to this next week.
Fr. Robert: I don’t expect you to do this next week, but what we do expect is that you understand that everything you see here, even for the functions that you don’t understand what they do, you understand how they work. So it’s using just the basics. You see for loops, you see variables here, you see mathematical operations. And then all it’s done is it’s created these functions that you can use, you can use in your program, to build on programming that’s someone else has actually made for you. It’s, again, it looks crazy. This looks like when we did the API, and people were freaking out. It looks ridiculous, but it’s—
Fr. Robert: Yeah. You get to do stuff like this.
Fr. Robert: That’s so cool!
Dale: Yeah, so that’s what that does.
Fr. Robert: So Dale, what was the name of this again? It was pyglet, you’re calling it?
Dale: Yes, pyglet.
Fr. Robert: We’ll make sure to give you links to pyglet in the show notes so that you can download and install this library. We know that it’s too much, and actually we designed it this way. The last two episodes of any module is gonna be the blow-your-hair-back stuff.
Fr. Robert: Right. But—
Shannon: This is the stuff for the experts.
Fr. Robert: That’s why in the ivory tower we gave you some additional basics. Go ahead and play with tuplets, and yes I’m still calling them tuplets. And if you are a more advanced coder, go ahead and download that library and we wanna see what can do with it. Dale Chase, Discovery Digital Networks. You know, Bryan, we’ve got a little something-something you can play while we say thank you to our code warrior, right?
Shannon: Oh, no!
Fr. Robert: Bring the audio down on that a tiny bit. Dale, this is you in your other life. You’re not just a programmer, you’re an artist, you’re a fantastic artist, you do nerd-core. Can you tell the folks at home where they can find you, where they can get your work?
Dale: Yeah, you can get all my music for free right now, still, at dchase.bandcamp.com.
Fr. Robert: Oh, I’m sorry, I’m just grooving on it right now.
Fr. Robert: It’s kind of hard not to, like, listen, because it’s awesome.
Shannon: It’s a good song. I won’t lie, it’s a good song.
Fr. Robert: But can you just turn this up a little, Bryan?
[song plays louder]
Shannon: He’s so smooth!
Fr. Robert: I know.
Shannon: Got that smooth voice. It just sounds so good.
Fr. Robert: Wait, wait, wait. Hold on, here it comes.
Shannon: You don’t want to hear this part. OK, you can turn it off now.
Fr. Robert: All right, Dale, that’s awesome man. Thank you so very much again, for bringing down the knowledge to our code monkeys.
Dale: No doubt, guys. Thank you for having me on.
Shannon: Thank you, Dale.
Fr. Robert: I know next week is gonna be our last, (sniff), our last Python—
Shannon: Don’t say that!
Fr. Robert: —episode.
Fr. Robert: So you’re gonna give us something to blow their wigs back, right? We’re going crazy, we’re going all out, just nuts.
Dale: Yeah, I guess I’ll try to have some fun with pyglet and see what I can show you guys.
Fr. Robert: Fanstastic. Dale Chase, Discovery Digital Networks, we love you as always. Thank you for being our code warrior.
Dale: Catch you guys.
Fr. Robert: All right.
Fr. Robert: Now folks, we know we gave you a lot of information, a lot, I mean, seriously.
Shannon: Don’t we always?
Fr. Robert: A lot. Yeah, but that’s why’ve got show notes. Shannon, you want to tell them about show notes?
Shannon: Sure. Twit.tv/code is where you can find all the notes, and we do upload all of our code to the Github. There’s a link to the Github at twit.tv so you can find all of our code. You can copy and paste it and run it on your own computers. So have fun.
Fr. Robert: That’s the best way to play, folks. You just gotta learn, you gotta poke, you gotta break it, and then fix it.
Shannon: That’s right.
Fr. Robert: And also don’t forget that you can subscribe to Coding 101 on iTunes if you’re an iCoder. We want you to spread the work about Coding 101 far and wide. We’re still gaining audience members by the thousands on every episode, so—
Shannon: Yay! Thank you so much, everyone!
Fr. Robert: Thank you so very, very much. Make sure people know that they gotta learn about coding. Even if you don’t want to be a coder, you have to understand how the tools that you use every day are programmed by geeks.
Shannon: I’ll be honest, it really does help when you understand, even just you look at coding you say, oh, that’s obviously C#. It helps if you ever run into problems downloading something or exporting document or whatever.
Fr. Robert: Totally. It absolutely does. Now, you know it’s not just iTunes and the show notes page that you can find us on.
Shannon: No, we are on Youtube as well. Youtube.com/twitcoding101.And you can go over there, subscribe to our channel, please do it, and also comment below on whatever you like about the show. And I read all those comments, by the way.
Fr. Robert: She does. She’s really good about it.
Shannon: I do.
Fr. Robert: You can also find us on G Plus. We’re actually almost at a thousand members.
Fr. Robert: Help us to get to a thousand members by the end of the Python module. We just need, what was it, twelve?
Fr. Robert: Give us twelve members!
Shannon: Oh my gosh!
Fr. Robert: Go on, go get twelve people to join the community. Now this community isn’t just a place where we hang out. You’ll find in there some really, really good programmers. In fact, I’m betting—
Shannon: That one at the top.
Fr. Robert: Yeah, look at that.
Shannon: He figured out how to download the daily, I think it was the Hubble space telescope. Isn’t that cool?
Fr. Robert: I mean, talk about taking data from an external source and turning it into something useful, boom! Folks, if you want to know the more advanced techniques of the programming masters, be sure to join our G Plus group. Where can they find that, Shannon?
Shannon: Gplus.to/twitcoding101. I think we also got the actual name now from google, which is plus.google.com/twitcoding101.
Fr. Robert: That’s right. And if you’re not into the G Plus group, you can always find us on Twitter. I’m on twitter.com /PadreSJ. That’s @PadreSJ.
Shannon: And I am @Snubs.
Fr. Robert: Yeah. And finally, don’t forget that you can watch us live each week, Thursdays 1:30 p.m. Pacific Time, here at live.twit.tv.
Shannon: We’re live.
Fr. Robert: Yeah. Yeah.
Shannon: There’s people out there.
Fr. Robert: There’s people out there. And in fact we can prove that there’s people out there by going into our chatroom.
Shannon: Hey guys! What’s up?
Fr. Robert: Find them at irc.twit.tv. It’s one of the ways that we participate. You see us pulling questions out of the chatroom.
Fr. Robert: It’s all TWiT love. Until next time, I’m Father Robert Ballecer.
Shannon: I’m Shannon Morse.
Fr. Robert: End of line!