字幕表 動画を再生する 英語字幕をプリント >> PARLANTE: All right, hey there. Hey, good morning. Welcome to the PyQuick Basic Python Class. My name is Nick Parlante and I work in Google's engEDU group which deals with sort of technical training in engineering. And also I have a job at Stanford where I work as a lecturer. So, the Python today is--or rather, it's actually a 2-day class. So this class is about just the basic useful normal Python. And Python is sort of--you know, it's a nice friendly learn--and you can actually learn a lot of Python in two days. So, that's the good news. This is the class where the prerequisite is not--you don't need to be like super-expert engineer to come in here and learn Python. What we want is just that you have some experience in some language. So, like, yeah, you have some idea what a variable is or something like that. And then, and Python's going to meet you halfway on this. Python, like I was saying before, you know, it's a nicely designed language. It--a lot of things work pretty easily and so you can learn a lot of Python pretty quickly. So, this is a 2-day class. And what's going to happen is it'll be a mix of lecture and coding sections. I'll kind of alternate between the two. The good news or I guess the bad news or whatever; the news is that the class moves pretty quickly. So, I'll show few things in lecture and, you know, kind of demonstrate couple of things. And I want to get pretty quickly to you trying that in coding because, really, when you code it up, you know, that's when you're going to learn it. So, as I lecture through stuff, don't feel like--I don't want you to have the idea like, "Oh,"--where you're memorizing or writing down everything that I say. So, [INDISTINCT] few here for a second, so there's a--for the PyQuick class, we have a whole set of written materials and here's the PyQuick page, it links all of them. And particular today, there's this one PyQuick Basics. And that's pretty much what we're going to talk about today. It's pretty long and it talks about strings and lists and modules and a bunch of things that I'm going to lecture about. So, my goal is in the lecture sections, just kind of pick up the basic idea of what I'm showing you. But don't feel like you need to memorize every detail of it. And then later, when we're in the lab section, yeah, you can go to the [INDISTINCT] packing it. You can go, "Oh, rightly, he said something about strings," you can sort of orient yourself a little bit and figure it out how this stuff works. So, there's also--I had, sort of--I printed some copies of this Basics thing but then I didn't make quite enough before class started, so I'll go get those when we're at the next break so I'll also give you a printed dead-tree version of this document for you to get started. Alrighty, so, let's just get started. So, Python is a--actually kind of an old language. It was created by a guy name Guido van Rossum, who actually works at Google, and it was created in 1990. So, I think about it as, like, a little bit old by, you know, hip language standards. However, in the most recent years, Python seems to have gotten a lot of momentums. It's becoming pretty popular. And I think it stems from--I get it, it's basically a pretty good language. I think of Python as being sort of a quick and light language. So, if I have some little tasks, some little automation, I just want to just dance, encode and be done. Python seems to work very well, like it's very, sort of, frictionless. It's just--quick little things just happen very nicely. I think you would categorize Python as a scripting language. So, it's similar to maybe Pearl or Bash or Ruby or maybe JavaScript, the whole sort of space of languages that don't have a real heavy type system. So, Python is certainly a nice example in there. Python as a--I'll show you as we start mess around with Python. Python is good at, sort of, quick turnaround. So, you could--if you have an idea, a little experiment you want to run, you just type it in and you could just try it immediately. There's not a big compile stuff or anything, sort of, slow. So, we're going to--I'm going to encourage you to have a development style where you have a, sort of, quick turnaround between editing and running and I'll demo that in a minute. So Python is certainly very appropriate for small projects, with little bits of animation. Within Google, Python gets used for all sorts of little things like that. There is debate about whether or not Python is also good for huge projects, since it lacks a big type system--and I'm not going to settle that debate today. But, you know, there's certainly advocates for both sides. But, certainly, as you know, Python is a wonderful language to learn for solving small encoding problems. Ah, all right. So, let me show you--a little bit of Python code here. Let's see. So, Python is an interpretive language. So, there's this program called Python. And later on, we're going to write Python programs, the Python--this is called the Python interpreter. It's actually going to, kind of, do the running for us. So, one of the nice qualities of Python is that you can just run the interpreter, so I just type "python" in here. And then, I can just type little snippets of codes, just add it and it'll, sort of, compile and run them just as we go. And so, this is a marvelous way to sort of see what's going on. So, I can just show you some basic Python here. So, I'll assign a variable "a," I'll say, you know, "a is 6." And so, what the interpreter does here is what's called the read eval print loop. So, I type a little bit of code to it. When I hit return, it's going to evaluate it and its going to come back with a prompt. It'll say, "Okay, I know you--now what do you want to do?" So if I type "a," just a value, what it does is it evaluates it. In some sense, it kind of prints like, "Well, here's, sort of, the ASCII form of that," and then it comes back with a prompt. This is a very standard interpreter sort of thing. So, a couple of notes; please notice there. Notice I didn't have to declare that I was going to have a variable "a" or that it was going to be an "int," no. Python, it's all about quick light. We're just like, get right to it. So, just by assigning to "a" that it cause us to exist. Later on, I could say, "Oh, let's have 'a' be the string 'hello,'" Well, okay. We're fine. So, there's not a compiled time type that associated with "a." Instead, "a" just points to whatever it points to. So right now--so I have to hit "a" here. So, yeah, so now it's a string, earlier it wasn't it. Though, with the jolly a little bit aligned, the way you can think about in Python is that a value, you know, "a" in this case, it points to something and in or a string. And whatever it points to knows what type it is. That's stored at runtime. And so, then, as the code runs, it just uses the types of the objects as they are at runtime. And we'll certainly see that theme in a lot of the cases. Okay, I'll show you just syntax, at least. So, there's a built-in function called Len. And so, if I wanted to take the, you know, find the length of that string, I could say, "Len of a," and okay, it turns out, that's "5." Also, Python is case sensitive. So, if I type an upper case "A" I'm going to get this error. So it says, "nah, blah-blah." This bottom part, I think it's almost English, "Name error: name "A" is not defined." Okay, well, that's telling you something. So, in Python, if it comes across a variable or some other symbol which has not previously been given some value, then that's an error. Then there's a little bit in contrast to some languages where if it's undefined, it's like, "Well, you know, let's just use the empty string." Or, you know, "Let's just kind of blunder ahead." Python is not that way. So, this will also come up like when you go out and bounce an array or do some other thing where it's a little bit like it doesn't seem to match up, Python will halt. And I think giving you an experience, the Python language is just--since the greatest source of delays in your code-working is bugs. And so, it's bad, or if it's been found, it's bad if a bug is, sort of, sitting in there, hidden. Really, if there's an error, you want to know. And so, Python reflects, or reflects that style. So, let me show--so, what's going to happen is we're going to do all sorts of Python. And the interpreter is a great source of doing little experiments. So, for example, what's going to happen is someone's going to ask me some questions about Python and I'm just not going to know the answer. I mean, of course. And--but rather than admit that, what I'm going to say is, "Oh, that's an excellent question. Hey, let's try it in the interpreter and see." And in that way, I'm never at risk of being wrong, or whatever, it's just going to do whatever it is going to do. So, for example, you might wonder, "Oh, well, what if I want to have a string and an int together?" So, I'll say, "'Hello' + 6." Now, what does that do? Okay, it turns out that doesn't work. I was mentioning the solution, so you're already at "+"--in Java that works and some of the--in Python though, "+" between a string and it does not automatically convert then it fix it. Oh, in the interpreter, by the way, the "Up arrow" works. So it's, sort of, like in Bash, I can, like, flip the previous lines. So it just happens, if I call, there's an "str" function. So, if I do "str" on it then "str" on kind of anything. Then that makes, or tries to make a string out of it and then the code works. I'll talk more about strings and stuff later on. But you can do that yourself, right? You can be the interpreter and if you have some questions about, "Oh, what if I do to--do the--where?" It's just so quick like, well, just, you know, fire up the interpreter and try it out. And so, Python does work. I guess it's in an interactive style. And I'm being a little repetitious about this because coming from a Java or C++ background, this feature isn't very foreign. So, it's not something you would necessary have an instinct to do. But in Python, it works very well. So I will certainly encourage you to go that way. Oh, all right. So let me get out this. I'll show you how quit the interpreter; the way I do it. What I do is I type the word, "quit," and it doesn't work. But in the error message, it says--"Oh, right, right, Control D." Okay, good. And so then I type 'control'--Oops. So, here we go. So, in our in--you know, later on, oh, you don't have to do it now, but later on we'll have this PyQuick directory. It's going to have a bunch of exercises for you to do. And I'll analyze my selling here. And in particular, I've got this "hello.py" file. And so, I'm going to use that as kind of a--just real basic example to start things up. So, one of the required, you know, in order to do this class, what you want to do is you're going to have a way of editing and running, and switching between those two very quickly. And there's a separate handout that talks about that. So, I won't repeat that in lecture. In this case, I'm going to have these two windows. So up here, I've got this like, you know, whatever, primitive EmEx editor but while I can do editing in that screen and then in this screen, I can do running. So, what I'd like to do is, you know, right here is a very basic Python program. I'd like to just talk about the parts of this thing. So, up here at the very top, you've have this standard user bin, Python, you know, sort of, or, you know, number sign--bang! Just talking about what interpreter is going to use this. We're going to--today, we're going to use Python 2.4. It's a perfectly modern version and this is also the official modern version used here in Google. There's also a 2.5 and a 2.6. But those differences are like not real big, so I wouldn't worry about those. I would not use Python 2.2 because that at--is actually different. So, 2.4. Go with that. Python 3 actually just came out and it does have some real differences; although, it's not in wide use yet. Here and there--mostly, we're doing just totally straight-ahead normal standard Python. And so, that's not very different in Python 3000--in Python 3. But in a couple of places, I may point out things that are going to be over little different in Python 3. But for the most part, the stuff we're going to do, we'll all come through fine. The "-tt" thing, I'll talk about in a second. Okay, let's get rid with this import system. There. So, what this says, this is a "def main: print 'Hello.'" That defines a function. So, you say, "def" and space and then the name of the thing. Yeah, question? >> [INDISTINCT] >> PARLANTE: Oh, I'm sorry. It's coming off a bit. Oh, thank you. There we go, all right. So, this is just a simple function definition. In this case, I can get whatever name I want. But it's very conventional in Python that whatever is going to be, kind of, like the main thing you do, you'll call lower case m, "main." In this case, it turns out Python has a print operator. And so, you can say "print" and then there's a series of things and separate them with commas and it prints them. So, in this case, it just prints out, "Hello." This thing at the bottom is a little unfortunate but I'm going to--it's boilerplate syntax. And that is the boilerplate syntax to run the "main," that we find above. And I will mention very briefly why this is here. But then, you should think of this as just something that's just mechanically you just put at the bottom of the file and then don't think about too much. The way this works is that a Python program can be--in this case, "Hello.py," you can run it like [INDISTINCT] like I want to invoke this program. And in that case, that if statement will be true, the way the interpreter sets things up. And so, since it's true then here what does is it's calling the main function. So, actually, I can just demonstrate that. So, if I go down here to my--same directory, so, the way you run this thing--well, there's two ways, you could type "python," the name of the interpreter, and then give it "hello.py," say, "Hey, please run that." And then, you could see here it prints, "Hello." So, there's my marvelous program functioning. In the Unix way, the more modern thing is that you would--you have the XQ bit set--I'll have talk about this a little bit, and so then, you can just say, that's the, you know, whatever. "hello.py" and it just runs it. So, either of those techniques will work okay. All right. So, in that example, I'm running it and so that then this if statement is trying out to be true and so then it runs "main." There is this other way that you can load a Python program. Where one Python module wants to load another one and use it essentially like a library--which I, actually, am going to demonstrate, I think, tomorrow. And in that case, I want to load the Python module, it's sort of synonymous with [INDISTINCT] file in Python. I want to load it but I don't want to run it. I just want to have its definitions available and so, in that case, this if statement will be false if I load it. And so then, it will bring the module in but it won't run its main. So, that's what the if statement is for. But for today, we're just always going to run it just like hello.py so you don't need to worry about this too much. Okay, so let me show you--I want to, you know, I going to add a few features to this thing; and so just to, kind of, show you some things. So first thing I'm going to do is, I want to print out the command-line arguments from my main here. It's just a very common thing to want to do. And it turns out, there's a module called "sys." And the sys module, there's a Python--standard Python thing, it includes a lot of, sort of, operating system interface-type stuff. So, you could access command-line arguments or you could exit the whole program or whatever. I'll show you how to bring up the documentation in a second. In Python, the way that you refer to some external module of stuff you'd like to pull in is you have an import statement. You say, "import sys" or import--I'll show you a bunch of different ones over the next few days. And then, down here, to refer to something inside of the module, you say module name, so "sys" and then dot and then and you'll just need to know the name. In this case, it's, "sys.argv," is the name of the command-line arguments in that module. So, I'm going to save that and then down here, I'll run, "hello," and I'll just say like "aaa bbb ccc," right? So, those are command-line arguments. So now, when I run it, see it prints these out. So that is a Python list. It has four--so Python lists have square brackets around them. And we'll mess with this a lot more later on. In this case, this list has four elements in it, which is, kind of, the old Unix standard way of doing command-line arguments. The first argument, it points to the script itself, so that's "./hello.py." And then, the subsequently three elements--these guys here, that's just--Oops! These guys then just correspond to the arguments here of that I passed at. So, what I want to show you--so, I'm going back to my source code here. So, so far, this part doesn't really do anything very useful. But I do want to show you just the idea of a module like, yeah, there's--we're going to use modules a lot today. So, that's how we're going to pull stuff, you know, you use codec way to write by code of my program. Now, a very reasonable question is like, "Oh, 'sys,' like, how am I supposed to know that?" like, "Where did that come from?" So, I want to show you two ways that you can, kind of, research what a module is and what it has in it. And I'm going to show you two. There's the hi-tech way and there's the easy way. And they both work fine. So, you could use either of these ways. First of all, I'll show you the hi-tech way. All right, so I'm going to fire up the Python interpreter, and actually in the Python interpreter, I can say "import sys." I mean, it really kind of looks like a program. It's just that I get to type stuff interactively. So then, inside of--there are two functions in here. One is "dir," so I can do a "dir" on sys. And what that does is to just, kind of, shows me all the symbols that are defined in there. So, it's sort of an homage to dash, I always think of dir. And so, you can see, you know, if you didn't know about "argv," or whatever, you'd say like, "Oh, exit." "Oh, that's probably the exit function," and--where's "argv?" Oh, all right. Check it out. There's "argv." So this--some of these are data, some of them are codes but you can at least get a feel for what's inside of there. So, dir, that's the first one; then there's a companion called help. And what help does is it kind of pulls up sort of a JavaDoc, sort of a man page documents about like, oh, what is going on with this module? And so, with dir and help, you can kind of poke around a little bit and orient yourself. And what's nice--if you look at my list here, for example, there's--this is my example, there's exit. So, that's sys dot exit that's a function. I can actually say help of sys dot exit, and then it pulls up just help about that function. So, just in Python and in lecture or whatever, I will say, "Oh, use the Ebola ABC Module," and I'll kind of mention a couple of functions that you need to use but I won't give you further direction than that. And so then, what you know, which is a very realistic position to be in. And what will happen is that in Python, you can use dir and help to kind of dig around and get better docs, or you know, using--using this technique. I'll do another example. Remember I talked about the len function earlier? So, I could say help of len and that like, gives you like, okay--not pretty scant description but a little bit of description about that. One thing I'll point out about this first of all, usual--notice why, when I type help len, I'm just typing len without a set of parentheses after it? And that's a kind of subtle syntactic distinction. When I say len paren hello like that, I'm calling the len function. But when I just say len with no parens like that, I'm just referring to the len function. I'm pointing to say, well, here is some code. Don't run it, I just want to talk about this code. So, here when I call help, notice it's just len and it's just unadorned, all righty? So, let me show you--so that's the high-tech way of doing dir and help, certainly very useful. Now, I'll show you the easy way. The easy way is you go to your browser and you just go to Google and you just type Python--like what did we just do? I'll say Python sys exit and then whatever; Google searches, for whatever reason, just work really well with Python. So, like the first couple hits, like yeah, it just is the docs for the Python sys exit function or I've been doing a Python--I'll show you strings in a second. So, if I say Python string, type that out, oh yeah, first hit here. So, Python.org is the official--so, Python, I should mention, is you know open-source and all, like good minor stuff. Python.org is sort of the official Python home and so if you see a link that's docs.Python.org a lot--you know that--Python.org has excellent documentation and tutorials and all sorts of stuff like that. And so, if you get a Google search that just kind of points into that a lot of times, so that's going to be nice authoritative answer. So, just doing Google actually works amazing alone. I was feel like I'm being kind of powerful and hip if I use like, dir and help to dig around, but I must admit that in fact, just doing a Google search that, like, any six-year old could do, like, in fact, works great. And so, you know, both those techniques are available, you have to use whichever one you like, all righty. So, let me show you--let me go back to my--get out of here. So, I want to build up the Python code inside of a--inside of this function bit, show you couple of things. One thing, I'm going to show you a very common error, real quick. So, I'm going to comment out the import sys. So, incidentally the number sign is the comment character, so like, and that just goes to the end of the line. So, this is a very typical way I would write this code. I'm using sys.argv, but I've forgotten to do the import. So, I want to show you what that looks like. So, if I say hello, I get an error. So, this is a Python error trace. So, the most interesting part here is maybe at the bottom, says, "Global name sys is not defined." Now, the error is not very specific here but it's kind of--it doesn't quite know if I wanted to have a variable name sys or module, it doesn't know. It just looks like S-Y-S to it, but it is that error like, while I came across the symbol and it was not previously defined. So, you could see that that is one of the basic rules of Python that if you--if you're going to use a symbol, it previously needs to have been given a value. So, when you see something like that and it refers to a module that you're trying to use, then what that means is--oh, right, I forgot to do the import. So, I go back here, I'll put the import back. So, essentially what the import does is it takes the symbol S-Y-S and it binds it to point to something so that then down stream, when you say sys dot whatever, it's able to see if it'll actually work. So, let's try that much and now work again. Okay good, now we're back to just pitching the arguments. All right, so I'd like to do--well, I'll show you how to build a real program out of this. So, I'm going to define a hello function that does something. So, I'll say hello and then let's say this will take in a string. So, I'm doing a second def here and let's say--let's say this is a name, actually. I'll say prints, "hello" and then the print--you can actually separate things with commas. So, if I say "hello" name--and you can have multiple comments, multiple things in it; it sort of prints those out and puts them on a line. So, what I'll do down here actually and when I'm all set, let's have--let's put a bunch of exclamation marks after the name. So, that's my little "hello" function that has two lines in it and I'll talk about the plus and--you know, I'm glossing over some details there, but you can sort of see what it does. And then down here, I'll call my "hello" function and what I'm going to do is I'm going to pass in the first command line argument. So that was sys.argv and that turns out the zeroth element refers to the script itself which is--that's just an ancient convention. So, the element that I really care about is sys.argv one there. So, I'm going to save that and I'll see if this works, so I'll say, "Hello Alice," and so there, check it out. All right, so now it prints "Hello Alice," you can sort of see the results there. All right, so let me talk a little bit. I just want to use this as a vehicle to kind of talk about what's going on syntactically with Python. So, I'll sort of o through this top to bottom. So, I give this thing an argument called name. Notice just like--just as with the variables, I didn't have to declare that this was a string or an ant or anything like that. I just say well, whatever; it's just called name and it just points to whatever it points to. So, arguments and variables are similar, they're both just names that point to some value. Then, in Python when you want set off a block of code, it very often uses the colon, as you see here. So, I'm going to say define this function and there's a colon. Now, I'm going to go to the next line and I'm going to have, you know, how are many lines of code I want to have. Now, this brings us to Python's most sort of famous syntactic feature which is that Python does not have any sort--any left curly brace, right curly brace notion to enclose a block of code, instead, Python uses indentation. So, notice under the def here--so the Google standard is to indent by two spaces, and so that's a fine thing to do for today. So under the def, this first line is indented by two and this next line is indented by two. So, that's what's putting them inside of that function and there just isn't a left curly brace or right curly brace I typed to put those in there. So, this is--it's a little bit controversial. I'll just give you my take on it. I think the idea is that if we were writing this in Java--I'll disturb all the real Python people here, you know. I don't know. Suppose it looked like this ... sounds really funny, all right. Suppose we had that, right? We're blocking out the code that makes this function and what we would've indented--like we would've had one set of brain cells that was managing the curly bracers and putting them in right. But then, we also would've indented correctly, right? And we were raised right and it looks terrible. So, of course, we would've indented correctly as well; that would've been a second set of brain cells, those. So, we would've maintaining these two things kind of in parallel. But if you start to think about it, those two bits of syntax would've been reflecting the same piece of information; they always would've moved in tandem. And so, the Python idea is like you know, having two bits of syntax that represent the same thing and then we're just trying to keep them in sync is dumb. Like, we want to just do the quickest, most minimal, clean thing and that's kind what Python looks like, and so it's like, you know what? Let's just get rid of the curly braces. If the--if we want to look right--so I'll put the colon back there--let's just have the indentation define how the blocks of code go. I'll fill this--this function have to be little longer so you'd get a better feel for what indentation looks like. My advice about this is that it seems extremely foreign, weird and slightly wrong for about the first 11 minutes that you use it. And then, it's just fine. I mean, it's just basically very logical and reason why--it's kind of visual and so, it very quickly just seems normal. So I encourage you just not worry about it. All right, so let me--I want to make this function a little longer; then allow me to demonstrate the indentation a little more. Yeah, question? >> It has space between hello and Alice [INDISTINCT]. >> PARLANTE: Oh, so yeah, the question is, where does the space come? That's a quality of print that when you separate things with a comma, it puts a single space between them. If I wanted to bunch those together, I could've done a plus. The plus in string space just crams them together, so that would've done it. All right, so, let me show you--so, I want to make this a little fancier here. I'm going to put in an If statement. So, let's see. I'll say if--let's say, if name and I know I'll just end up showing you how Boolean this stuff work. If name is, let's say, Alice, then we'll say--I'm just going to--this won't be very meaningful. Here, we'll add some question marks on there. So the--I'll just start at the top here. So, this is what an If statement looks like. You say if and then you have the test and then there's a colon and then it has whatever--block of statements underneath it. Now, just to get with this idea of indentation, if I wanted to also do multiple things, I could say you know, you know, I'd consider, you know, "Alert: Alice Mode" here or something. The fact that those two lines, the print alert and these two lines are underneath the If, that's what's putting them, you know, under the control of that if statement. And then here, when this line is indented back to Alt that--that's what putting me outside the If statement. So, this is what it means to say that the indentation is significant, right? Where something is placed left-right does control what the code means and so that's the foreign part. All right, so let's start about this If statement. Here, I'm using "= =." "= =" does sort of an intuitive deep comparison. In some languages, you kind of have to worry, well, is this a point of comparison or whatever? In Python, you do not to have that worry. "= =" does what I have described as a kind of a real comparison. It takes the thing on the left, the thing in the right and it really fleshes them out and compares them. So, it works for integers, it works for strings; later on, I'll show you it works for entire lists. So, you could have relatively big data structures and just compare them with "= =" and you'd still get a reasonable result. In C++ or Java, you always got to have the parentheses around the test. In Python, the parentheses are not required. And then, so it's regarded as the best Python style to not put them in. It's a little bit like--it's like speaking with your sort of vulgar accent. Like if you put the parens in, then everyone's like, "Oh, I see. One of you is, you know, C++ refugees." So, you could sort of show off your modernity by, like, not putting in it. If you want to have a logical connective, this is a little bit strange. The logical connectives are spelled out with letters. So, or is O-R and is A-N-D and not is--it's a little hard to say in English--not isn't the thing above the one on the keyboard, its N-O-T. So, I'll say, you know, if name is, you know, Nick. So, finally, I'll put a--I'll put an else in here just to show off. So I'll say else colon--what do we do in the else case? I'll print "Else." Okay. So, now you that--you could sort of see the indentation and you're really working here and we've got this kind of modest Python program. All those mentioned, you know, all the regular Boolean stuff. You know, there's less than and equal equal and, you know, all the kind of regular Boolean stuff works in Python as it does in other languages. There are Boolean true and false values, although those aren't used very often. The rule in Python is that if you have appointed (ph) or something, there's a few kind of null values that count as false. So, if a number is zero that counts as false. If a string is the empty string that counts as false and any other value or, you know, and the--or if the string is not empty or if a number is not zero, then those count as true. So, that's similar to how other languages do it. All right, so let me just try running this a little bit. So, I guess if I run this and I say "Hello Alice," then my If statement kicks in, so I get all those question marks and then the line below kicks in and I go--oh, it's pretty doofy, pretty doofy looking out, but hey, at least it works. So, I'm going to show you--I'm going to make a very sweeping claim about how Python works. Python does everything at the last possible second. So, in C++ or Java, it takes your code and it compiles and it checks it in a million different ways and you might get a lot of different compile errors, and then only after all that compile checking, then it runs it. That's not how Python works. When you feed your Python code like my hello.py in the Python, it just looks at it in the most superficial way, it checks pretty much nothing; it just starts running it. And each line it gets to, it evaluates at that line kind of in the moment. So, in the moment, it looks at what those variables point to and oh, he's got a string and an ant here and it just tries to do it just at the last possible second. And only at that second does it notice if there's an error, maybe a variable is not defined or you're calling a function that doesn't exist or you know, dividing by zero--I mean, we're always, you know, aware of bugs. So, to highlight that, I want to try calling a function here. I'm going to call a function called DoesNotExist. Here, I'll pass in a name. Now, there is no such function. I haven't defined it, there's not a built in code DoesNotExist, it's just wrong, all right? That else is just incorrect. This does demonstrate a deep quality of Python. Now, if I call hello.py and pass Alice, you know what? It's going to work fine. All right, this is what I mean. What happens is Python only checks a line when it runs that line, all right? And so, because it just kind of never hit this else, whatever, it just kind of never knows that that was wrong. Now, in the great, you know--as we understand the universe, you don't get something for nothing. Well, in Python it means there's not a big barrier between you and just, like, your code running. And that can be a real virtue in a lot of cases. But also, you're giving up a certain amount of checking that some languages might have done. This also means that in a more industrial sense, because Python doesn't really check a lot at a compel time, for industrial code, it's more important for it have to have good units test coverage. Like you actually need to have run all the lines to see that they're correct. Now, for our two days, we're going to work on things that are just kind of medium sized little utilities where, you know, if they're incorrect, you can see pretty clearly and so for that kind of problem Python is really a good match. All righty. So, let me show you--I think I'm going to go back to the Python interpreter here. And I've showed you--I've sort of used strings and lists and a few things informally, but I never really explained them, so I want to kind of slowdown a little bit here and actually talk about how some of these things work. So, the Python string-type is enclosed in quotes. So, there's like this string "hello" I've been done. You can also actually put the string in double quotes or--here I'll do a different one. Isn't--oops--that works the same. There's not a deep semantic difference you could--it's just your choice, you can either use single quotes or double quotes. The one difference is that inside of a double quoted string, as I have here, you can put a single quote like isn't there and it just, just works. And, likewise, if I'd use single quotes, on the outside, I could put a double quote inside. There's not a real strong preference. You can kind of use which the one you prefer. I tend to use the single quote one. It's just, it looks a little Python to me. Yeah, question. >> What kind string that you want include single and double quotes? >> PARLANTE: Ah, in fairness to your question, so if want to--so let's say I'll do a double quoted one and I want to put a double quote inside of it. So say like--I--what would you say, I'd say like--I--what you do is you put a back quote, a back taken from it. So I'd say, "I love this exercise," yeah, that's fine. Well, alternately, I could have done it, and set some quotes. All right. So, I should you--let's see what's a--isn't. So I showed you the "len." Oops. The "len" function earlier. So strings also––as I think I've already done, they--the plus works. So if I'd say "a + you know yay" then that puts it together, you know, plus put strings together to make a bigger string. Strings in Python are what is called immutable and that means that once the string is created it never changes. It's like a little read only thing. That's a pretty modern style that turns out to be pretty popular in a lot of languages. So for an example when I did that little plus there, if I look at the original "a" it's unchanged. All right, the--so whenever anything you do with strings, lowercase, uppercase, I'm gonna show you a few things. It's always creating new strings to sort of show you the result, but the original string it's always left unchanged. So, well, maybe I'll do is I'll set "a" to let's say uppercase "hello." Now it turns out there are a bunch of built-in string what are called methods and I'll show you one now. So for example there's one called "lower" and what that does is it makes the lowercase version of a string. Now the reason this is called a method and this is basic object oriented programming just as a--you know there's some--a few things that are very easy and that's we're going to work on for the next couple of days. "A" this is, it points to the string, all right, I could think of a string object and with this syntax where I say pointer to a thing dot and them the name of the method I want to run. What that means is run that method on that object. So if I had "b" is equal to like "yay," so if I say "b.lower" okay, well, it runs on that string. Whereas when I'd say "a.lower" then it ran on "a," so that's basic, it's, it's, it doesn't, it's a different syntax for running a piece of code called the method on an object and that's you know hopefully pretty--that's done pretty intuitively. So, what I was saying before is that strings are immutable, they are never changed. So for example, if I look at the original--so here, I'll do it again. If I say "a.lower" that's not changing "a" that's returning to me a new string that's the lowercase version I wanted. d if I look at the original "a, it's unchanged. Now Python has--there are many built-in string methods. I showed you lower. I can show you--there's like a find, just for example. So if I say "a.find of "e" and what that does is it searches in the string for the first occurrence of that and then returns to me in the index. Now in reality, there are probably dozens of built-in string methods and I'm not gonna demo them all in lecture. I just want you to know that there are a lot of built-in ones and either you could look at the Python (ph) where it talks about some of the common ones, or you could type, you know, in Google, type, Python string, or maybe Python string method and go to the page where it list them all. One of the theme--you know one of the reasons that software is a lot more productive than it was say 15 years ago is that we've gotten better at having what I think sort of built-in codes. Like a code you didn't write that, but that you can call and it just solves basic problems for you, sort of code reuse, and Python like any modern language actually has a lot of code already done for you. And so one of the basic skills--I think it's kind of living higher on the food chain is when you're solving a problem, you don't necessarily like, "Oh, now I'm gonna manually ride a loop to solve that." Very often your first instinct is to find the module and dig around a little bit to find some code that somebody else already wrote, it's already done and you're going to build your solution on top of that. That is, that's a sort of good modern technique and it's an excellent Python technique. Python has a lot of built-in stuff and you know over the cost of few days, I will certainly point you where a lot of that code list. All right. So, I haven't talked about how to look inside of a string. It turns out you can use a square brackets to look inside of a string. So if I'd say, "a [0]" that's the left most character in "a [1]" that's the "e" and so on. If I go out of bounds here then that's an error, so I really need to adjust you know keep within the bounds of the actual thing. So one last--some text--I'll show you for this--this is a little less--so you can use the plus to put together. You know if I wanted to have a string and whatever, I could use the plus to put together. There's another form that uses seized print-up syntax where I could say like "hi % s" and the "% s" is a place holder where I'd like a string to go and I could say like, "I have" you know "% d donuts" and so that's, that's called a format string and it a sort of--it's a good way of mixing in. You have this outer skeleton and you want to sort of substitute in a few things and so then you use the "%" sign and then you combine it with what you want. So here I'll say, "Alice and 42." And so using this sort of "%" construct, you can have a string in sort of substitute values into it into make a bigger string. Does not it really ever required, you could always have gotten that result using plus to kind of put the string together but, it's fairly common to use this so I'll just, I'll just mention it. The last thing I'll say about strings is that, the strings that I'm showing you right here these are Unicode strings. Python's Unicode treatment has been like a little uneven and particular in Python 3 it changed a little bit, it got better. So for here I just want you to realize like, yeah, these are not Python strings. These are not Unicode strings but--and in the handout talks about there's a slightly difference route by which you create Unicode string. But once you've got a Unicode string then all the things I showed you still work. The square brackets, the "len", the ".lower," all that kind of stuff, it's the same interface is just you created or so in a different way. And so, I will--but for our purposes this will work. These strings essentially are just serious and bites, it's just a (buck) of bites. All right. So that's almost--I'm going to show there. So, I want to, I want to show you this one--how are we do on time? Oh, excellent. I want to show this last string feature and I want to block out time for our first exercise. So, in order to show you this, I'm going to go to the py quick basics document. I want to--very high tech here. I'm going to attempt to--here we go. It's just possible. I'm going to try and show the interpreter in a little bit of art that I put in there to end up. Okay, oh, perfect. All right. So here, I'll say, "a = hello," so I've got the same string. So, in that little piece of art, all I've done is I've drawn in the index numbers, so like all the languages in Python, the things are--if I want a number and when your sequence of things, they are numbered from zero, starting at the left. So, if I'd say--if I refer to "a [0]" that's the "h"--this is how I should have done it for or "a [1]" that's the "e." Or if I say, "What's the len of this thing?" that's "5," so that just sort of conventional indexing into something. Now Python has a syntax for referring to not just a single element in a string like this but a kind of subpart of it, and this syntax is gonna use the colon. So the way this is going to work is if I say "a" and I'll put the square bracket but I'm going to put a colon in the middle here, and I'm going to refer--I'm going to indicate both the start and the end. So, for example, if I'd say, "What, what?" I want you to start at one; so that's the first index number and then the second number--yeah, it's going to say, it's going to go up to but not including that one. In Python, this is called a slice. So if I'd say "a [1:3]" that's the subpart of the string starting at the one and going up to but not including the three. This is called--it's only entitled called the slice. There's a--the word Pythonic is not a word I just made up, it's a real one, in the nerdoroty (ph), and this is a very Pythonic feature. Python likes having syntaxes which are sort of very short but crisp but then again you know it express something that a common thing that you might want to do. So, if I wanted to--if I wanted to say, "Hello," I guess, I could say, what is it, a one--oops, "a [1:5]." That's a little weird. The five is kind of one out of bounds there but, actually, what you could do in the slice syntax is if you omit the second, the thing after the colon, it just goes all the way through the end of the string. And, if you omit the first one, it starts at the beginning. So actually it is a truism that if I'd just say square bracket, colon square bracket, well, I just get the whole of whatever it was. So this is a very heavy syntax. For say, for example, if I want to, you know, remove the first level--level--letter. I could just say one colon (ph) so like instead of starting at zero, start at one and then just go through the end. So that's, so far that's like pretty neat. But now, just a little bit of scrolling, I'd show you the slightly other crazy thing it does. So, thus far, I've just used the positive numbers, but in Python, they also added a second set of numbers that also index into the strings. So it's just using the negative numbers and the negative numbers instead of starting at the left, they start at the right. So minus one refers to the rightmost character and minus two refers to the next one and the minus three and so on. You can think of essentially as there's an implicit len there that--the minus one essentially saying len minus one, right. So len in this case is five. Len minus one is four. I'll check it out for you. So do you have written a code in a zero based system but it means that referring to things that happen to be at the left is very convenient because like zero, one, two, you always know where you are. But then referring to things at the right is like a pain because you always have to add len and subtract something. So the negative numbers scheme just also makes it convenient to refer to things on the right hand side, and they work in slices too. So, for example, how about I say that I want to omit--first, I'll just do my earlier example. So I could say what is it? "[-4:-2]"--oops. I'll put the "a" there. That's exactly equivalent to the one where earlier it was a one colon three, right. The minus four is just another way of saying that. I'm going to get a more realistic example. Say for example, I wanted to omit the last three characters of the string. I didn't know how long it was. I could just write that as colon minus three. All right, that it's going to go up to but not included. I know minus three is the third in. So like I could "he" or I could do a getaway. What if I only want the last three characters of a string? I guess right that is minus three colon. So, it just--I think, syntactically, this is maybe it looks a little bit weird but I found--the slice syntax, I find it--it's just useful in a lot of situations. So I would encourage you to go ahead and actually learn this one and we're going to have all these exercises and stuff in a little bit. Certainly, I have hidden inside of their little opportunities for you to end up going to use the slice syntax. So that is a nice one to get the stuff going. Okay, so that actually concludes the first lecture section. So what I would like you to do is pull up the exercises and so if you go to the py quick page--just do it here--it points to this page, py quick exercises, here you go. And then that explains how you copy and I--if you could raise your hand, I'm happy to kind of walk on. How you can copy--this is--said directory, direct, directories. It's going to look like this and let me--now, let me show what I want you to do. So inside here, there's like day one, day two, and some other stuff. Today, we're doing day one. So go in the day one directory, and the first thing I want you to look at is there's this, there's a file there called--oops, no, not that one. There's a file there called "string1.py" and I'm just going to look inside of there. A so what this thing has--is in the comments--sorry, I'll make this a little bigger. There are some little exercises in here that just use the stuff in lecture that we've done so far. So, for example, here's, you know, exercise "A: Dr. Evil" and just in English, there's a little description of like what it is I want you to do and it gives a little example but then the code it just not done. And so your job is fill in the code there to actually compute what it's suppose to compute; there's a few of these things. And then at the bottom, I have filled in little bits of test code. And so you see that you don't have to touch that, that's already done. They're just going to call the functions above and just kind of check that they were trying the right thing. So it's kind of a primitive form of unit testing. So if I run this thing right now, what it does is they all fail, because yeah, there is no code and so what's gonna happen is as you fill those bits of function in then somebody's pass--this discussion start passing. You can just run it each time to just very quickly just get feedback about how it's going. So that is string one, I'd like everyone to do string one. If you are just so fast, you get a little bored, there's also a string two. In string two, it just contains more. And so, optionally, if we you have more time to kill, I'd be happy for you to go ahead and do that. The last thing I need to point out here--let's see, I'll just look at string one here. There's a thing I mentioned earlier that I never explained. This "-tt" flag. What that refers to is in a Python file, what I would recommend is you just always indent with spaces and that that is the Google standard, just always indent with spaces and you're okay. But, in your editor, maybe by habit or whatever you might accidentally hit the tab key, and if you have a Python file that has a mixture where it uses and spaces in some places, in tabs and other places that is deeply confusing, because visually where the codes appears to vertically appears to a line may not correspond to how it really aligns as far as Python is concerned. So what the "-tt" flag does is if it ever finds a mixture of spaces and tabs, it just immediately halts, which is for--when you're on your first day of Python programming that's absolutely what you want. So this will help you find the case where you want to do is the [INDISTINCT] I've talked about. You want to set your editor so that if you ever hit the tab key, it understands to just put in spaces. So put the "-tt" flag will protect you if, if you end up with--make a mistake with that. Oh, all right so here's what I'd like to do, it's now, it's about 10:50, so I'm going to imagine; I want you to work on this with me maybe about half hour for that, so that's--what I'd like you to do is then go have lunch. So what I'd like you to do is be back in here--I'll have do all the math here. Back in here at 1:15 and so I'm going to leave you some time for doing Python coding and then some time for having for having lunch. You, it allows you to bounce something and of course, you know, I'm in here to answer questions or whatever during that whole time. All right. So, so please go ahead and get started with that.
A2 初級 Google Pythonの授業1日目 前編 (Google Python Class Day 1 Part 1) 242 25 mike に公開 2021 年 01 月 14 日 シェア シェア 保存 報告 動画の中の単語