Google Python Class Day 1 Part 1 - 6
Google Python Class Day 1 Part 1 - 6
Google Python Class Day 1 Part 1 - 6
com
>> 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
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 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,
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
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.
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
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. Subtitles End: mo.dbxdb.com