2.3 - Expressions - Part 3

توضیح مختصر

They gather some input, maybe from a file or from a web service, they do some work to it, and then they produce something. The string ends up being input in the variable inp, and then it continues to the next line. usf, mnemonic variable name, United States floor.

  • زمان مطالعه 7 دقیقه
  • سطح متوسط

دانلود اپلیکیشن «زوم»

این درس را می‌توانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید

دانلود اپلیکیشن «زوم»

فایل ویدیویی

متن انگلیسی درس

So now we’re going to combine everything in the previous lectures and we’re going to 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 will 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 going to help you a lot. They might help your teaching assistant, but they’re also going to help you. And comments, comments are a way for us to add text that’s ignored by Python to our programs. 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, you know, 10 lines long, that’s a little complex and you want to say, “Oh, 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 and then, instead of deleting the line of code. And sometimes you do that for debugging. You add debugging code. And then when you don’t want the debugging code, you just put a pound sign. In case you want to take the pound sign out, and turn the code back on. So we turn it– I 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. And what I’ve added here is I’ve added four comments. I’ve added comments that basically help us understand what’s going on. And remember, I call these like paragraphs. Our first paragraph has human-readable code, human readable-text. Get the name of the file and open it. That’s what’s going to happen in the next two lines. These next five lines are count the word frequency. Maybe I should say make a histogram. So, make a histogram and you can just read this. And so, again, don’t think of this as like the teacher’s telling you you’ve got to write comments. Think this is as like, “I’ll write comments so I remember what I was doing here.” Why, why did I write this code? So write the comments to help yourself out. And now here, the last thing, we’re all done and we’re printing the stuff out. OK? So that’s comments, that’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. Right? So you are writing comments so that the future you can read the comments because 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 because I said so. Write only the ones that you find useful and don’t do something like x = 1 # put 1 in X. That’s kind of 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. One of the things they would do is they would read this and check to see if it really did what you said it was going to do to help you debug it, for example. So, so that’s comments. Documentation, very important. OK. So now we’re going to do our first programming code. Now, the pattern that’s going to 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. And 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 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 0 floor. And the 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. So if I’m on floor 7, what would be this floor if I was in United States? And that’s the problem we’re going to solve. Now, it’s probably not going to make us rich if we build an app for this but perhaps someone can get the European elevator converter app into some app store. And maybe you will get wealthy after all. But, for now, it just is 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 do with Python, it has to do with you or me reading about this. Then, input. 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 input in the variable inp, and then it continues to the next line. And it works on the right-hand side. We’re going to pull this string 0, we have to convert it to an integer. Otherwise, we can’t add 1 to it. If we did– if we just inp + 1, we’d get a traceback. But we say, int of inp, convert it to an integer. Now, if you, if you mess this up and put Bob in, then this thing is going to blow up because int can’t convert Bob to an integer. But, because we have a 0 here, we’re OK. So then we add 1 to it and store that in usf. usf, mnemonic variable name, United States floor. inp, the input we got from the user that– I’m using good variable names here. So, I– we store this in and so, you know, we got that becomes 1 and then we print out the US floor, comma, 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. OK? Now, there’s lots of things left to do. And most programs aren’t one line long. And there’s a lot of work that we’re going to 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 precedence. Focused a little bit on division where we talked about Python 2 versus Python 3. Type conversion and comments, and then, writing an entire program. So, up next, we’re going to start talking about conditional execution using the if and the else and other of the reserved words. And this is where some of the intelligence starts to seem to come into computers. So, up next, conditional execution.

مشارکت کنندگان در این صفحه

تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.

🖊 شما نیز می‌توانید برای مشارکت در ترجمه‌ی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.