>> PARLANTE: This morning, I talked about strings, they're just the most basic stuff,
and then we had the string exercises. So the time for the rest of the day I'm going to
talked about lists and tuples, what are their structures. We'll have--had a little exercises
about that, and then I'll finish off talking about dictionaries and files, chart tables,
and then we'll have a large exercise, and that will complete the day. So that's in line
for today. I'll probably get you out here around, I don't know, 4:15 or some like that.
All right. So let's talk about another Python type, so let's fire up the interpreter here.
So I did strings. So the next topic I'm going to show you is the lists. So the lists is
in a--appears in square brackets. So if I wanted a lists of like the numbers one, two,
three, that will look like that, like--I mean I could tie it into a variable to make it
very much like my last example. The elements in a lists, they often are of the same type
but there's no requirement, so I could have like "1, 2," and then like "aaaa",--oops,
okay. That's probably--oh, three commas. I'm sorry. So, it'll give a list of a, you know,
different types. In reality, mostly, you'll have a list of all the same thing for just,
you know, just sort the most intuitive case. Now, Python has this sort of design idea that
instead of having, you know, one syntax for strings and another syntax for list or whatever,
Python tries to use the one syntax very consistently for all things, and so this is a nice quality.
It means when you're learning it, there's less to memorize. So, for example, the length
function for lists is just the one we learned before. So, yeah, I could say "lan(a)", just
like for strings. And in fact, many of the bits of syntax or operations for strings were
countless as well. And so Pythons are deliberately consistent. So, for example, I could do a
"+". So I could say, well, I have the list "[1, 2, "aaaa"], you know, "+ [3, 4]". And
like with strings, we've seen that, right? It puts them together to make a bigger string.
With list, they're like, put some together to make a bigger list. So one difference that
list have--I'll set this back to "a = [1, 2, 3]. Is what is--what is the equals to?
So if I say "b = a", "end. The key thing to remember here is this does not make a copy.
What this has done is there's this one list sitting in memory somewhere, and it used to
be that "a" was pointing to it; and now, "b" is pointing to that same list. So there's
one list, and they're both pointing to it. The way you could tell, all right, we do a
little experiment--oh, I'm sorry; I really didn't to talk about. How do you refer to
the--an element inside of here? Yeah, it's just like, just like in the string. So, if
I say "a [0]", that's the first element, remember? So I can check my experiment by using this
on the right hand, so I'll say, well, I'll set "a[0] = 13". I'm going to change that
first element; lists are mutable, you can change them. Unlike strings, Strings are the
ones that don't' change. So if I look at "a", it's now different. But if I look at "b",
it's different also. So this kind of shows how they were just pointing to the one thing.
If you want to make a cut--mostly, you're just having the one list or whatever it is,
and having pointers sort of sprinkle through your [INDISTINCT] all pointing, all sort of--I
could say, sharing that data structure. For most part, that's just totally fine. You shouldn't--[INDISTINCT]
block that out. I will show you how to make a copy, just if you wanted to. The way you
make a copy--or actually, I'll set "b" to be a copy of "a"--is of you--the Pythonic
way is use slice syntax. So if you say, "[:]" that's the kind of Pythonic way of saying,
"Well, here's a linear quantitative things. Please make a copy of it." So, now "b" relays
a copy if I change "a" and "b" would change. Yeah? Question over here?
>> So the question was, if you'll be framing a copy for me to change "b" and also change
anything...? >> PARLANTE: Yeah. So the question is question
is, so before I made I copy, if I change "b" or if it's "a"? Yes. So there's this one list
in memory, they're both pointing to it. They're literally sharing it. Now, I'm going to say,
"Look, as a practical matter for your Python programs, there's just not a problem. It's
just fine." In C++ or in C, you would want to make copies mainly because of the memory
management, like trying to know when to free, you know, when to reclaim the memory. But
Python, like, you know, any reason about language has a garbage collector. It just takes care
of the memory collection, and so as a result, this need to make copies, just kind of goes
where--when I found transitioning from CC++ language is, to Python I found that it was
making a lot of your copies, likes the need do that had just gone away. So I guess what
I'm saying is like, you may have an instinct to make copies, where I'd say, attempt to
keep in check, the key you'll find, it just not make copies and most stuffs just going
to work fine. All right. So let's see we got the square brackets, the length, the--let's
see, what are "a" and "b" this days? Oh, they're the same. The "==" does comparisons just like
string. So you have two lists. It actually follows the list and checks piece by piece,
which are the same. So as before, it does have a kind of an intuitive notion of checking.
Now, what's the other thing we did lists, right, or I'm with strings [INDISTINCT] was
slices. That works with list as well. So right here, I've got these lists. If I say "a",
you know, I don't know what, [1:3] or whatever, I can pull a slice out of the list. So for--maybe
the most common case is, like, if I want to talk about a list, except or without the right
installment. So, I'll wait until we get to the exercises, but, yeah, you've got this
very concise syntax for kind of talky about some parts of a list. All right. So let me
show you the main syntax for this thing. So a very common thing to do--actually, I'm going
to--I'll write it over here and I'll I speak louder, a very common thing to do is to loop
over a list. And so, in--for example in C, or right, you've maybe written that loop where
you say, you know, "for i = 0, i< something", in Python, it would be very rare to do that.
There's just a built-in for each list--for each construct that just loops over a list,
and it's very commonly used which is I'm going to--if I actually walk over here and write
it out, you'll figure must be pretty kind. All right. The first word is, you begin with
"for", and then just click whatever variable name you'd like. So I sort of underline that
list, and then you have the word "in", and then you have whatever the list is, and then
a colon, and then you just do--you know, whatever it is you wanted to do for each element in
the list. So, for example, I might say, you know, "print VAR" or whatever. So this is
just a built-in way of looping over a list and doing something for each one. So, for
example, now, here I've got "a", so I might say "for num in a:"--now, here, I'm going
to write it all on one line just because I'm in the interpreter. The Google [INDISTINCT],
the more correct thing would be to have a colon and then go to the next line, but it
turns out you can't just crunch it under the line as I'm doing here. So if I say "for num
in a: prints num", then what that's going to do--the role of "..." there is just an
artifact to the interpreter, just to let you know there's more up that's coming, so you
can ignore that. So really what that did is it just looped over the list and, you know,
once for each element, it's going to run this body of code, and each time through that value
"num" is just going to be, you know, a one element from the list. So, this is a, you
know, it's the very common case. You just want to loop over the thing, you know, sort
of for each. Yeah, question back here. >> How does it know that numbers are also
those numbers? >> PARLANTE: Well, so let's do it, so the
question is how does it know that num is also those numbers? What its doing is num is just
a variable name just chosen by me, it could have been anything, and what it does in the
loop, essentially, what it's doing inside the loop is like num is equal to whatever,
you know, kind of like, what the next value is. It assigns it into num, just like a variable,
and then after that, then it runs with a little bit of code I have, and then it loops around
and does the next one. So in other languages, it might--it's similar to the idea of an iterator.
So, this is for--the exercises of this will come up late. The idea that you have a list
of a bunch of things and you kind of want to do something for each one, that is--that's
a common case and this is certainly the syntax you should use for that. There is a related
syntax--this is like this is the first one. The second one is very simple which is just--you
have some value, and you want to, you want to check if it is in a list, and you can imagine--I'm
sorry, is this too low down? You guys can't see? Sorry. I'll write it up above. I'll put
up here. Let's stuck this here, all right. If you want to check if a value is in a list,
you can write this way. You just put whatever the value is and then the word "in"; just
the "in" by itself, and then whatever the list is. So it's very short. So, if you've
got a--I mean, this works for list. It actually works for a lot of things, just "in" in Python.
There's this built-in way that you've got some composite data structure, and you want
to tell, is this thing in here? So what's "a"? So, for example, I can just write "2
in a", and that's just going to go over "a" figure out somehow, or with what something,
like, let's say the "14 in a". There's the "False". So there's another one--this sort
of higher in the food chain things, you might have an instinct, "Oh, I want to see if it's
in here. Oh, I'm going to write a four loop, and I guess I'll loop through, and I'll compare
each one with equals, equals rarely." I'd be like, "no". In Python, you've--it's just
built-in. You'll say, "Look, is this in there?" It works for actually a lot of data structures,
and in--the [INDISTINCT] case it's actually quite efficient, you know. It does--it does
something smart depending on what data structure you have. So you should not worry about doing
something with them. So, along with slices, you should just--so I'd rather that you just
go ahead and memorize those. Those two syntactic constructs are very common, you should just
know those. And, of course, my examples later on, so up here all sorts of time. All right.
So let me show you just as with the string, we had methods like ".lower" and ".replace"
and ".find", whatever. The list is the same way. It's got all sorts of built-in stuff.
I'm just going to show you a couple. One is there's an append--and you can just guess
what that does. Just to--puts it on the end there. One important thing about append is
that it does not return a new list. It returns the special value of none which I, you know,
you saw in the [INDISTINCT] a little bit. None upper case N--I'll just type here--is
sort of the null value. It means like nothing. So, "a.append", it returns nothing. What it
does is it modifies the list. So, it does not--it would be wrong to write this, "a
= a.append(xx)", you know, say, well, I have to say no. I mean there is this follow the
world where--I mean some other language maybe that were, but in Python that just happens
to not work at all. And it's because append does not return anything. It modifies the
list in place, and that's--I've absolutely made that here myself. It's just the--there's
this two different paradigms they could have chose, and they chose the one right at that
one. All right. Also, there's a--it's kind of the reverse of append, there's pop. And
so if I--this is by position. So if I say, "pop(0)", what that's going to do is it pop--it
removes, it sure pops up the zero with element, and returns it to me, and it modifies list.
So, it kind of removes it from the list and send it to you. So, you want to kind of remove
an element and just kind of get it out of there. Then, pop--pop [INDISTINCT]. And were--and,
then you should look at the docs. Yes, there are dozens of built-in list methods for all
sorts of common stuff. And if you have a problem involving manipulating a list, searching it
or replacing or whatever, yeah, you should go look it and go find the built-in ones.
There is a particular case though that I want to show you and that is--well, actually--I'm
sorry. There is--there's one--another one that I'm going to show you, which is something
I haven't talk about so far. So here is A, there's a--I guess this is an operator called
del, I mean, delete. And what delete does, obviously, it deletes. But, particularly,
you can use it on a variable. So if I say del A, what that does is it kind of removed
the definition of A from my local scope. Whereas, when I design A to six or whatever that I
put in there. So, now, if--after that if I try and use A, it's like, "Oh, that's not
define." So it's kind of the reverse of the direction I've tried before. And what happens,
the del also works on list. So if I say A is equal to one, two, three, and let's say
if I can say, well, you know--if I can say "A[1]", that's the two, right. So in terms
on how you can use a del to actually retain the list, so if I say del of "A[1]", that
kind of deletes out on that list, but now, the list actually shrinks. So that's--I've
shown you how to add, I've shown you how to pop, also del has another way. You can surf
the one there. Yes, question over here. >> So if you do--if you design A to B for
instance without copying it but actually point... >> PARLANTE: And so this one--in these cases,
we're actually just doing the interpreter. Okay, let's say B is something like it's 12,
and I say A equals B. >> And then you del A?
>> PARLANTE: Okay. And so if I say del A... >> B is still B.
>> PARLANTE: ...then B is still fine. >> Okay.
>> PARLANTE: All right. The del A--what it does, it's just kind of removes--I mean, it's
sort like when I had A and B point at the list, right. It's like, well, the list is
still there. Deleting A, I'm just like--I'm deleting the idea of A, but whatever it pointed
to, something else might have been pointing to as well. Now, with the 12--I use the word
point there as a little inaccurate but the basic pattern holds. All right, so the most
interesting thing with list I want to show you is sorting. So there's this old way of
doing sorting. Well, there's actually a dot sort method on the list and I want you to
not use that one. I'm going to show you this newer, cooler way that it's just better than
everyone. And you can--yeah, with all those dinosaurs were using dot sort you can just
like look down on them. All right, so what I'm going to do--well, first of all I'm going
to show you the most basic case. So let's say A equal to you know, four, two, one, six.
So the simplest way to do sorting is there's a function called sorted. And you can feed
a list into sorted, and what it does is it makes a new list and it sorts it, sorts it
in increasing order. Now, so in this case when I call sorted on A, it's making this
new list and then it sort again. In this case, we're getting a numeric sort. In reality what's
going on is it's the comparison in Python depends on the type of thing being compared.
And in this case, it's a list of int. And so when I gets down to the comparing one int
to another it sees like, "Oh, this is two ints, so I should do a numeric comparison.
If, instead, I have the list of strings, it would have gotten down to comparison with
the scene, "Oh, I have two strings," and so it would have done a textual comparison. Again,
it has that sort of Python quality like at the last possible second it sees whatever
the two types are and it does something appropriate, I guess. So one of the things about sorted--and,
actually, I'll pull up the help page for sorted. Remember help? So if I say "help(sorted)",
it's pretty [INDISTINCT], "sorted(iterable)". So this actually works for a list. You can
actually feed into sorted any--an iterator--some other things that kind of--are capable of
producing list so that they are not themselves list, you can feed those into sorted too,
it just works with all of this, anything that kind of looks like a sequence. So these syntax
here were first two optional named arguments. And so this built-in, like--yeah, you can
just not specify CMP or key or reverse, but if you do specify them, then, you can sort
give these extra arguments. This is a nice quality in Python. So in this case, I had
"sorted(a)". In that way, I could also say, "sorted(a,..." and then one of the optional
argument is called reverse. So, I mean, I have to go look up and see if that was the
name, but I see the name was reverse. And if I pass through for that, then, it still
sorts them but I just get it the backwards way. So that's a very common problem. I see
you just want to sort the other way so, and so that's the built-in. Yes, question.
>> I just need to ask to see whether A gets assign a sorted list or gets [INDISTINCT].
>> PARLANTE: Yeah, sorted makes a copy. It makes a new one and sorts that.
>> [INDISTINCT] into a new... >> PARLANTE: Yeah. Yeah. So for example--well,
so here is A, it's unchanged right? >> Yeah.
>> PARLANTE: And here's "sorted(a)". What I could do--this is what you're suggesting--is
I could say A is equal to "sorted(a)". Like yeah, I don't care to keep the original on
that, that's fine. So now, it's sorted. So you have the--once we spend over it you would
even don't have a choice right, so this way you get to pick. All right. So let me show
you just like basic sorting. So, now, I want to show you this little more sophisticated
about how to do custom sorting in Python. And the Python syntax--the Python structure
for this, I think, is really nice, but it's going to be a little more complicated. All
right, so I'm going--and I'm going to set up my case, so it's exactly the same as the
one in the handout. All right, so here, I'm going to have some strings. So if I say "sorted(a)"
here, the strings what--by default what I get is textual sort. Now, the problem I want
to consider is--for custom sorting, right, you want to sort some other way, not just
textually. The case I want to consider is, well, suppose I want to sort this by length,
right. I want to D first and then the B's and then C's, right. I want kind of increase
in order by length just for--just as an example. How might I do that? Well the old way to specify
custom sorting in a lot of languages is that you write what's called the two argument competitor.
You provide some function that takes two arguments and compares those two and returns either
a negative number or zero or positive number depending on how you wish for those to be
ordered and you feed that into the sorting function and then uses it. That is a classic
technique, the two argument comparative technique. But it's a little messy, it's a little difficult
to describe, and it's just not that great. So Python, we're not just going to do that
way. I'm going to show you a different way. And the idea is I'm going--here's a piece
of art in the handout. So in this case at the top, I've got my original list. And I've
got this idea that what I want to do is I want to sort by the length. So the way it's
going to work in Python is I'm going--you--we are going to provide a function of one argument.
This called the key function. And this function of one argument, then, what the system is
going to do is it's going to run it over our original list. And for each element in the
original list, it's going to call the function of one argument and it's going to get a single
value and it's going to build up a list of those new values. So I'm going to kind of
walk over here kind of point at my drawing. So here at the top, we've got my original
list. Now, imagine the function of one argument that I'm going to use in this case is the
Len function. We've been using it all along, right. And Len, when you call it on a string,
it returns the length to that string. So I'm going to give it--I'll show you the syntax,
but I'm going to say, "Hey, use Len as the kind of key function in this case." So it's
going to call Len--what is it--five times, and it's going to get this numbers right.
Three for the string length--three for the string length, just gets this kind of shadow
list of new values. So--now, here's where the magic happens. What the sorting is going
to do when I call sorted, I specify key function, it's going to make the shadow list and then
it's going to sort the shadow list. So the one is going to come first, right, and then
the two, whatever, it figures that out. And I got to make this hand waved motion that's
why it's kind like sorting motion, but then it applies it to the original list. It actually
shifts around the original elements. And so the result is that I get to the bottom. I
get the original elements but sorted according to the values of the shadow list. Now, it's
a little hard--yeah, it's a bit much to follow, but I'll tell you, in my experience this is
a much more convenient way to do custom sorting. You just set--sort of gets your brain around
this idea projecting out the shadow list. So before I take questions, let me just demo
that working. Show you what that looks like and then we can--then, we can work on some
of the examples. Right, so there's A, and here's Len, it's like a function. It's a lot
like a function. So what I'm going to say is what I want is "sorted(a)", and I'm going
to give this optional argument called "key=..." and I'm going to specify a function to use,
in this case I'm just going to say, "Yes, use Len." So if I do that, then, it's under
the hood. It's kind of projecting out that list. Now, let me do a second example, and
I'm going to change my list a little bit. Let's changed--let's changed "a[1]='aaaz".
I'm going to mix my problem up a little bit here. Let's say, "How do I"--I want to sort
this by the last character in each string. I want to ignore all the other characters.
Just give me--this is the sort of problem, there's a custom sort that you might solve
with the key equals function. And I'm going to do it with a def just like what you've
done. So I'm going to say "def Last(s)", and we're going to do this on one line. There's
nothing--because I'm the interpreter here. So let's say, def Last(s): return s[-1]".
Okay. Now, last is fine. So, now, if I say, "Last" just like so, like, "Oh," that points
to a function just like if I say Len, okay, yeah, that's function. The one that's built-in,
the one's I just made, but it's still going to work. All right, so the last function,
you give it a string and it just return that last character. So, now, if I say sorted so,
now, I remind what is that? So now, if I say "sorted(a, key=..." just the thing I just
defined, "Last", then check it out. So, now, I've done custom sorting. And in this case,
what it's doing in the shadow list is it just pulling out whatever the last, you know, the
minus one--whatever the last character is sorting by that. So there's a couple example
of custom sorting. Yes, question. >> So if the last time when A was DD, would
it release by factors DD, would it [INDISTINCT] like do the first sort by number of characters
and then within if there were two? >> PARLANTE: Actually, yeah. So I'm going
to answer a slightly different question. So--I mean there's this idea of, "What if I want
to sort by one thing and then I want to sort by something else?" You know, let me--I can
answer that more effectively when I've covered one more data structure. There is a way of
doing that, but I'd like to just hold off on it. I mean, it's a very reasonable question.
>> [INDISTINCT] what, would it just be random if you didn't use that extra [INDISTINCT].
>> PARLANTE: I don't think it would works. Do you want? But what I preferred to, I'm
going to save time and show you the way that it does work, then, we can talk about it,
okay. And so that's why unsatisfying, but there's these other data structure we'd really
need for this. All right, so I understand--I think the sorted thing is one of the more
difficult things, kind of kick your head around. But, yeah, predictably, the exercises have
like a bunch of custom sorts and it's a very powerful technique. So I'd say hold off on
that. And then I do have this beautiful art in the handout so try and--try and follow.
So just hold on with that, on the next set of exercises which will be pretty soon, you'll
get a chance to play with that. All right, so let me show you, just a couple other things
with the list. So one--I'll stick with A, just these strings. One fairly common thing
to do once you've got your program and you want to now produce your output is you want
to--you want to make a string out of list. So, like, one thing you might do is maybe
loop through this list, right? I could type "for s in a:" and, you know, kind of print
each element or something. It turns out, that's okay but there's a built in that just takes
the list and sort of concatenates or whatever all the parts of it together just in one step.
So that's such common on our version; that's nice to know. So, for example, if I were to
say, so this is called dot join. So if I were to say ':'.join(a), what it does is it just
puts it all together in one string just in one step. And so it's a little unintuitive
but actually the case I most use--use most often is backslash n; like I say new line,
dot join, and then really I've now got one per line but just all in one step. And then
I can just print that in one step or, you know, write it to a file or whatever. So that
is a handy one to know. I'll switch back to the colon 1. So going in the other direction,
there's also a split. So actually here, I'll say b is equal to that so now b is the string
I'll put together. If I say, ':'.split(b)--oops, was that wrong? Oh, I'm sorry, no, it's the
other way. Sorry, b.split(':') so you have a string, you called dot split and you say
here is essentially the delimiter. Then it kind of explodes it out so you can recover
the original list. So split out either--you know these are really not actually deep but
this just happens to be a common case. So those are handy to know. Oh, I should mention,
those are not regular expressions; those are just substrings. We'll do regular expressions
tomorrow. Yes, question? >> Can you talk about something really simple
which is like, what if you were going through a list of data and you're making a list. Maybe
here you are just defining this but >> PARLANTE: Yes, yes. Yes, here, so let's
do a little example like that, yes. So the question is how do you kind of make a list,
yes. So one pattern that shows up in the exercises is like let's say I've got "a" here and I
want to loop through this and I want to--I want to pick out some of them and put them
on some other list. A pattern might be like this where I'll say a result is equal to the
empty list and then I'll say something like for s in a: and I going to kind of--I want
to add some of them to the results or whatever. The way you would do that, I'll just add them
all in this case but I'm going to loop through it. I want to put some of this. I would just
use append so I'd say result.append(s) in this case. So now if I look at results, I
mean, essentially what I've done is I've just copied all this over. So in terms of--that
is a common pattern. You start with empty list and you kind of put some stuff in it.
>> I guess, like let's say you're our data or whatever, you don't have any list and your'e
starting your for loop... >> PARLANTE: Yes, yes. So the question is,
yes, what if you have documents, I mean, what you are getting at is realistic. The trick
is I'm going to show you how to open files and read text out of it and then I think at
that moment I'll answer that question. But, yes, thus far, I'm just doing list a little
bit. You know, I haven't gotten to that yet. All righty, so the last thing I'll show you
is just for completeness; I haven't showed you like a traditional for loop like i equal
zero one to the--you know, like in C or java script, you might do that all the time. Mostly,
the ones I've shown you, these techniques just cover it. However, there--I will show
you how--if you want to count a bunch of numbers, the way to do that is there is a function
called range. And what range does is it sort of on--if I say range 20 it's sort of on the
fly appears to build this list, starting at zero and running up to but not including that
number. So, if you want to count from, you know, zero to nineteen, you could say for
i in and then call range to kind of on the fly make this list appear, and so that would
be a way. Now, there is a little bit of difference between Python 2, 4 and Python 3, whether
that list is really created in memory or is it fake-created. The more modern version is
just fake-created or you know which saves memory. So you can--you should not worry about
that efficiency detail because the more modern versions are just all over it. Now, I have
shown you just the most simple case of calling range where you give it one argument but in
fact you can specify the starting number and the end number and should it go by fives,
all those kinds of permutations range supports when you give it more optional arguments.
And so if you want to generate a series of numbers, that would be the way to do it. There
is also a while loop which now I'm just going to demonstrate but yes, it works like while
loops in other languages. The most common case though is the one I've showed you, just
the for loop to loop over our question. Last thing I'll have to say about the for loop
is that it does have this constraint which is when you are looping over a list, you cannot
modify that list in a way which changes its length or structure. So you can't add an element--it
kind of makes sense, if you add an element in the loop, you could kind of get this weird
infinite structure. So do not modify while iterating over. Incidentally, Java has that
same constraint, sort of, based on--there is an implementation reality underneath there.
All righty, so we are almost ready for another exercise. The last thing I want to show you
is the tuple. So, I showed you strings surrounded by quotes, lists surrounded by curly braces.
The tuple is actually--it's a modest little data structure and it looks like this. Tuple
is put in parenthesis and has commas inside to separate the elements. So a tuple--now
a list can grow and shrink, right? It can start as length zero and I can append, put
stuff in there, delete, whatever. The tuple is fixed-sized. You create it length three,
it's length three forever. In fact, it's immutable. You just can't change it. So what--the tuple
is good if you have some fixed number of items. Like let's say you want to store an x, y,
z coordinate in three dimensional space, right, there's never going to be a fourth number.
You know it's just these three, I want to lock them together. Tuples are good for that.
Or let's say you have a URL and a score and it's these two things so, like, I want these
two things to go together. In Python, you can put those on a tuple length 2 and store
that on a list or return it from a function or whatever. So the tuple is the kind of anonymous
way of taking a fixed number of things and just kind of locking them together so you
can pass them around or store them or whatever. So the--and in the later exercises, you'll
see some examples of this. So I'll sign a to this tuple I guess. And then kind of like
you could guess, I'll make this claim that Python is all consistent. So how do suppose
you tell the length of a tuple? Of course, it's len, right? And how do you pick an element
out? Yes, it's square bracket. So all that stuff list, strings, you know, pretty much
works for tuples as well. The one difference is the tuple is immutable. So if I say a square
bracket zero equals 13, I try to sign into it, that's going to fail, right? So the tuple
can't be changed. Once you create it, it's like a string. So tuples and strings, immutable;
lists, mutable. There was a question earlier about sorting. And it turns out, if you want
to sort by one thing and then sort by another thing, the best way to do that is with tuples.
And so I think I can do a short example here so I'll say 1, comma, a, comma, so this is--I'm
making a list of tuples here. Oops, okay, let's make that b and then here we'll make
this 2, comma, a. So, the way it's going to work is if you sort tuples, just the natural
under-the-hood sorting, what it does is it first compares the first elements of the tuples.
And if they're different, it just sorts by that. But if those elements are the same then
it goes to the next one. So here, and I need to make this--and I would just say 1a. All
right, so what I've made is a list of tuples. And to show this off, I think if I do sorted
on that, where the first number are the same, it goes to the second, right. So in the two
1 cases, then it is sorted by a b and then this one is last. So that is a little bit
of a detail. It's not something--I haven't planned on mentioning this but for the question
back here, if you want to sort on one thing and then sort by something else, what you
can do is write your key function to return a tuple. And then in--by whatever you put
in that tuple, you can get the behavior you want, you know, put the thing you want to
sort on first, that's the first thing, and then a comma and whatever the second thing
is and it will just go automatically go to the second thing if the first thing is the
same. And that is just built-in Python sorting, like it gets how to sort tuples that way.
All righty, so one other thing I have to show you with tuples, this is kind of, dare I say,
kind of a Perl, I think it is sort of a Perl feature is that if you say (x, y), there's
a special thing with assignment and variables, if you say (x, y) in tuple equals (1, 2),
what it does is kind of a parallel assignment. So now, x, x is 1 and y is 2, not super common
thing to use but it does show up now and then. The--okay, now, there's more stuff though
I'll show you later about that. All righty, so what we've seen really at this point, so
let's take in, is list, how to loop over them and dot append or whatever and sorting and
a little bit about tuples. So what I would like you to do is take a look at the exercise.
I'm sorry, question? >> Yes, does slicing work with tuples?
>> PARLANTE: Yes, slicing works with tuples. So what I would like you to do is take a look
at the exercise list 1 dot P Y. This is going to involve doing basic stuff with lists and
also a little bit of custom sorting. As before, if you are just so unbelievably fast that
you finish those then there's a list dot 2 dot P Y. But mostly, I'm just hoping people
can, you know, get at least some of the problems done in list 1 dot P Y. And what I'd like
you to do is work on that, say, for half an hour. So I'm going to pull you guys back here
at 2:20. I'll do one more lecture and then we'll have significantly bigger exercise till
I finish out the afternoon. Okay, so, off with coding.