Google Python Class Day 2 Part 4

Uploaded by GoogleDevelopers on 01.03.2010

>> PARLANTE: So, I just want to make a couple sort of concluding remarks and say, tell you
some stuff about Python. I want to show you like, one more Python feature I've been kind
of holding back on and then we'll let you go to--go back coding or go have some more
free food. So, the first thing I'm going to do--oh, I just have to establish the point
of order. All right, so, what does readable code mean? Let's think about that. It's like,
well, I would say it's code that if someone who didn't write it, they kind of look down
at the printout and they think for like a--they can kind of see what it does. And I'm willing
to pause; we'd agree that readable code, that's a good thing. All right, we like code that
you can read. All right, so I'm just going to table that, I'm going to put that on the
side for use later into this discussion. All right, so I want to show you this feature.
This is a feature I kind of thought about showing you. But there was just so much going
on. This was just one feature too many for our discussion. But this is something that
you're now ready to use. So, we've done a lot of stuff with lists. So, I'm going to
set up a list here. I'll have like, you know, "aaaa", "bb" and "ccccc", that's it. All right,
there's a list of strings. And suppose I wanted to have a list of their lengths. We've seen
how--I could've said, "Well, you know, result, the sequel to the empty list." Then I could've--you
know, I looped through list A and added stuff to the result list like. And you know what?
That is a fine technique. But Python has a feature called a list comprehension that is
a syntax for just making lists with a very kind of compact syntax and just making them
in one step. And so, I want to show you the list comprehension syntax. And so my--the
problem--my first problem will be like, oh, what if I want to make a list? Link three
of the lengths of those strings, so, kind of parallels the list of strings. So, the
list comprehension syntax is like--I'm not sure what adjective describes it and so on.
But anyway, here's the way I do it. So the list comprehension syntax, what you start
off is you write a set of square brackets and then you're going to put code inside of
there to represent the list that you want to compute. This is the way I do it, so I
type the square brackets and I go into the middle here. And then it--the list comprehension
syntax, it reuses as much of the existing syntax as possible. So, the ones--the existing
syntax we know is the "for" loop. So, I say--oops. I say, "for s in a." all right, for each string
inside of there and then, for the list comprehension syntax. Then on the left hand side, you put
the value that it would like to use. What function would you like it to run on each
s in this case? So, I'm going to say, "Oh, yeah, what's len(s) for s in a?" And if I
just hit return there, that just-- in one step, just makes the new list. So, if you
wanted to have--you have a kind of a list of one sort, this is a--in a list, you would
call this a mapcar. If you have a list of one sort and you want to kind of transform
it to a list of another sort, then this is exactly what the list of comprehension does.
Now, let me show you another example. I'll show you another feature it has. So, if I'll--let's
say I have a list with, you know, one, two, three, four here. Oops. All right, and now
I'll write a comprehension. I'll say, "for num in a." And let's say I wanted to do squares,
so I could say, "Oh, num times num for num in a." So, I could just make the squares that
way. You can also add an "if" statement on this if you want to kind of do--what you would
maybe call a filter operation. So I could say, "Well, I want num times num for num in
a, if I don't know, num is greater than two." And so now, this kind of runs over the list
and you're sort of slicing off some elements and then computing this thing for it. So let
me--I'll show you just a last example. Let's see, what directory am I in here? Oh, good.
Oh right, I'm in the copy special directory. So, as the last example, we have that, you
know, import os. So we have that "os.listdir" thing, right? I'll say "listdir(".")". All
right, so there's that list there. I'm just going to--can we just write a comprehension
to just make the list of special files all in one step? And I'm kind of pushing the envelope
a little bit here. So, I'm going to say, "import re." So here, I'll do the square braces. So
I want to say, "For f," I'll say, you know, for the--this is the filename--"for f in os.listdir(".")"
Okay, so now I want to say--I just want "f", so it's "f for f in os." And then I want to
add an "if" statement here, right? I want to say, "if" And what was it? Like
"r"__\w+"..." This isn't cryptic at all. I think that--so, let's just try it. Now, check
it out. So, there's a bunch of things kind of getting compacted in there, right? I mean,
there's the search [INDISTINCT]. Okay, so for small problems, the list comprehension
is very attractive. And it feels like you have this idea in your mind--it's always nice
if you have your--an idea in your mind, and then the amount of code to kind of express
that idea is small. You didn't feel like you needed to add all this extra barricades and
stuff to kind of like get your idea out there. And list comprehensions definitely have that
feeling. Although I think the syntax is a little foreign at first. I mean, but you know,
pretty, you get kind a little bit uncomfortable typing out. And then the mnemonic for what
it's worth is that it reuses the "for" loop and the "if" statement as they exist and sort
of just puts them in a square bracket. That's kind of the way to remember it, except for
the thing about the expression on the left that you just have to commit some brain cells
to remember that one. All right, so list comprehension is very attractive and you know, according
to the Google style guide, we would be very happy to use these as so long as they're one
or two lines long. And now, I have to give you this warning, this surgeon general required
warning whenever you are showing someone the list comprehension. And the warning goes as
follows. There is this form of madness that can take over a programmer. And the madness
is of this form. You had a program and it was like 100 lines long, and your officemate,
you know, used a bunch of macros and weird features that no normal person ever uses and
they got it down to like 80 lines. And you saw what they did and you're like, "Oh, that's
nothing." And you like, use more and more esoteric features or whatever kind of stuff
and you sort of get it down to like three lines. You know, it's just like unbelievable.
Like and it is so satisfying. I mean, it is actually very satisfying to feel like your
cleverness is being--just totally working and wow, you have this thing like completely
condensed. So, the problem is that it's a path, it like, pulls you on a path. But in
reality, what you've created is completely unreal, right? It is not that someone could
look at that and say "What does that do?" So, the list comprehension, I mean, it's like--it's
like that scenario I described is a risk factor. So, the list comprehension is great for like
a couple lines long. But what you can do is you can nest them. You can have an inter-list
comprehension that does something and then that's nested inside of another one, inside
of another one. You can sort of take your entire program and just have it be like this
nested list comprehension. But I would definitely recommend that you not do that. Or as I would
say, it's kind of like with great power comes great responsibility. So, this is a very nice
feature. If you feel like it's still readable and sensible but just like, you know, don't
go out of control. Yeah, question? >> Is it faster?
>> PARLANTE: Is it faster? I don't think it's really, really faster, not--not especially.
I mean, the trick is you figure that "for" loop like the "for" whatever append. That
is a highly optimized case. That's a super, super common thing to do in Python. So, I
think these are both low optimized. I don't think slower either. I mean, I guess maybe
it has some slight advantages, but I don't think it would be significantly faster. Great,
so, I just wanted to show you that, it's just kind of for fun. The other thing I'll mention
is that--well, I just want to talk about, you know, Python and you, what does this mean
a little bit. So, Python is actually a pretty popular language and its popularity seems
to have gone up a lot in recent years. And I think, certainly in this class, you've gotten
kind of a feel of--in a sense, I've tried to give you assignments or projects where
Python looks pretty good, right? So, Python has--it has the nice--the list is nice, the
string is nice, the dictionary--so anything where you're kind of pulling in data and using
those things where Python is good, those are going to work pretty well. I think that's
one area of strength. Also, syntactically, it's pretty short. I think that may you know,
you're not typing a lot extra stuff, and so that makes it attractive for small problems.
And then finally, like a lot of other languages, I think Python--you benefit from the urllib
and records, you know, all those kind of modules that are sort of out there that you can build
on top of them. And so, yeah, out in the real world, if you look at scientists or universities
or companies or whatever, yeah, a lot of people find Python to be like a really nice fit for
problems they actually have, and so, I'm sure it gets used a lot. And certainly with Google,
it gets used for like, all sorts of stuff. And I think, I will speculate that, you know,
probably the amount of languages like Python or JavaScript or Java or whatever is just
gradually on the way up in Google as they save programmer time and they use a little
more memory, a little CPU, but you know, ultimately, programmer time is something that we, is extremely
expensive and rare. And so, you know, something that allows us to save programmer time is
a little bit attractive. So, what I would say--and so, I'd have to give you one warning,
so Python is in fact a large language. If you really looked at all the features that
this language has, there's a lot to it. And we have not, we have not covered all those
things. That--just study. The good news is you have absolutely seen the core of the language,
like the very common, straight ahead, normal features that you would want to use to solve
a lot of problems, you have seen them. And in fact, you've written projects that use
a lot of them. So, I would say from here on out, you should be very comfortable if you
are--want to read a Python book or go some Python talk at Google or whatever, like, yeah,
you've definitely seen the basic core stuff. And so, I think you're--well, you know, well-set-up
to kind of go from here and sort of learn more Python if you want. And my last piece
of advice about that is that I found I--the way you learn this stuff is by writing code,
right? I mean, you can watch talks, but really, it's about in coding. And certainly in this
class, you know, hopefully write--somebody is solving these problems, you learn something.
And so, for myself, or say for example, within Google, I--my Python stuff got stronger when
I started forcing myself to solve things in Python. And so, the first offender there was
really Bash. So, things that I might've solved as a shell script and especially--I think
Bash works well if you're just calling programs and just sort of dumping their output to text
files, I go to that--that's fine. Or like, I can write a correct Bash script so long
as it has, at most, one "if" statement in it. It's like I can sort of get that right.
But if you find yourself in Bash like reeling the data in and sorting it and uniqueing it
and cutting it into tabs and all, it's sort of doing data processing in Bash, then I think
Python maybe looks pretty good. But if what you could do is instead call the program from
Python but pull the data into Python for processing. So you can use regular expressions, and [INDISTINCT],
all that kind of stuff. So I think that, as a first next step for Python programming.
I think that's a good thing considering, you know, definitely Python gets used in Google
a lot. All right, so that concludes--that's the extent of the Python teaching we're going
to have today. So, thanks so much for your attention, and I'm happy to stay longer if
you want to work on the projects and good luck with Python in the future. All right,
thank you. Thank you.