Skip to 0 minutes and 9 seconds So now we’re gonna combine everything in the previous lectures, and we’re gonna actually make our first program. So before we make this program, we need to talk a little bit about documentation. We talked about mnemonic variables as a friendly thing for humans. Another thing that’s a friendly thing in programs for humans and you’ll tend to realize that the human that you’re being friendly to when you’re writing code and you’re doing it well is yourself. So good variable names are gonna help you a lot. They might help your teaching assistant but they’re also gonna help you. And comments. Comments are a way for us to add text that’s ignored by Python to our programs.
Skip to 0 minutes and 49 seconds And so it’s a great way to give a little idea what’s going on in code, maybe there’s a couple of lines coming up that are ten lines long that’s a little complex and you wanna say all this is really doing is reversing the order of these two things, or who knows what. Or you might document who wrote the code, and you can actually just take a line of code, and put a pound sign in front of it, instead of deleting a line of code, and sometimes you do that for debugging. You add debugging code. And then when you don’t want the debugging code, just put a pound sign.
Skip to 1 minute and 19 seconds In case you want to take the pound sign out to turn the code back on. So we use it as a way to turn off and on lines of code. So here is our word frequency that we keep coming back to. What I’ve added here, is I’ve added four comments. I’ve added comments that basically help us understand what is going on. And remember I call these paragraphs. Our first paragraph has human readable code. Human readable text. Get the name of the file and open it. That’s what’s gonna happen in the next two lines. These next five lines are count the word frequency, maybe I should say make a histogram.
Skip to 1 minute and 54 seconds So make a histogram, and you can just read this. So again, don’t think of this as like the teachers telling you you gotta write comments. Think of this as like I’ll write comments so I remember what I was doing here. Why did I write this code? So write the comments to help yourself out. And now here are the last thing we’re all done and we’re printing the stuff out, okay? So that’s comments. It’s an important part of documenting your code so that you can figure it out later. And again, the human who’s going to like you for doing all this stuff is you, right? So you, this is you, and you’re giving a gift to the future you.
Skip to 2 minutes and 32 seconds Right? So you are writing comments so that the future you can read the comments cuz in a day or a week or a month, you won’t quite remember what you were doing so go ahead and write comments. But don’t write them cause I said so, write only the ones you find useful and don’t do something like x = 1 pound sign put 1 in x. That’s kinda silly, I mean everyone can figure out what x=1 means. So you don’t put silly comments in, just for silliness. You just put them in, so that you can especially when you have to understand what’s going on here, so that people don’t have to read quite so detailed.
Skip to 3 minutes and 8 seconds One of the things they do is that when they read this, and check to see if it really did what you said what it was gonna do to help you debug it for example. So that’s comments. Documentation, very important. Okay, so now we’re gonna do our first programming code. Now the pattern that’s gonna happen in here is a pattern that I call, and I was taught 25, 30 many years ago - input processing and output are the essential things that computers do. They gather some input maybe from a file or from a web service. They do some work to it and then they produce something - in work out. The work is the hard part usually.
Skip to 3 minutes and 48 seconds So this program is our first program that demonstrates all three of those things, input, processing, and output. And it’s a three-line program with one comment. So that the problem that this program is trying to solve, for those of you who have traveled in the US and traveled everywhere else, is that the ground floor in hotels in most of the world is the zero floor. And a ground floor in the United States is the 1 floor. And so you might find yourself in a European elevator, asking, what floor, I mean, what is the equivalent United States floor to this floor that I’m sitting on?
Skip to 4 minutes and 27 seconds So if I’m on floor seven, what would be this floor if I was in the United States? And that’s the problem we’re going to solve. Now, it’s probably not gonna make us rich if we build an app for this. But perhaps someone can get the European elevator converter app in to some app store and maybe you will get wealthy after all. But for now, it’s just as sufficient to teach us about a full blown program that does input processing and output. It’s as simple a program as I could make. So let’s take a look at it. A comment convert elevator floors. That has nothing to do with Python, has to do with you or me reading about this. Then input.
Skip to 5 minutes and 9 seconds Well, remember input prints out the prompt and then pauses and waits. And then we type, and then we type the enter key, right? And then this 0, this is a string. The string ends up being put in the variable inp. And then it continues to the next line and then it works on the right hand side. We’re gonna pull this string 0, we have to convert it to an integer. Otherwise we can’t add one to it. If we didn’t if we just inp plus one, we would get a traceback but we say int of inp converted into an integer.
Skip to 5 minutes and 44 seconds Now if you mess this up and put BOB in then this thing is gonna blow up because int can’t convert BOB to an integer. But because we have a zero here, we’re okay. So then we add one to it and store that in usf. usf mnemonic variable name, United States floor, inp, the input we got from the user, I’m using good variable names here, so we store this in, and so we got that, that becomes one, and then we print out us floor, usf remember the comma produces this little space down here. And so we have our input processing and output in a way that builds us an application, okay.
Skip to 6 minutes and 23 seconds Now, there’s lots of things allowed to do, and most programs aren’t one line long, and there’s a lot of work that we’re gonna have to learn. But this gets us a start. So we’ve talked a lot of stuff. We’ve talked about constants, we’ve talked about variables, we’ve talked about reserved words, we’ve talked about type, we’ve talked about mnemonic variable names which are both wonderful and a little confusing at the same time. Operators, operator precedents, focused a little bit on division where we talk about Python 2 versus Python 3, type conversion and comments and then writing an entire program. So, up next we’re gonna start talking about conditional execution. Using the if and the else and other of the reserved words.
Skip to 7 minutes and 8 seconds And this is where some of the intelligence starts to seems to come into computers. So up next conditional execution.