Python for Informatics - Chapter 10 - Tuples
-
0:00 - 0:05Hello and welcome to Chapter Ten of Python
for Informatics, the chapter on tuples. -
0:05 - 0:10I'm Charles Severance. I'm your lecturer
and I'm the author of the textbook. -
0:10 - 0:14As always, this material is copyright
Creative Commons Attribution, -
0:14 - 0:18including the video lectures, the slides,
and the book. -
0:20 - 0:24So tuples are the third kind of collection
that we've talked about. -
0:24 - 0:26We've talked about lists and
-
0:26 - 0:27we've talked about dictionaries.
-
0:27 - 0:30And in the dictionary lecture, we kind
of alluded to tuples. -
0:32 - 0:34We don't have to talk too much about
tuples, it's really -
0:34 - 0:37shortening the lecture by telling you that
they're a lot like lists. -
0:37 - 0:43They're a, non-change, they're a
non-changeable list. -
0:45 - 0:49And, and the syntax of them is
-
0:49 - 0:51pretty much the same as a list except
-
0:51 - 0:56that we use parentheses instead of
square brackets. -
0:56 - 0:58Okay? And so
-
0:58 - 1:00like here is a three-tuple, a tuple
-
1:00 - 1:03with three items in it, Glenn, Sally, and
Joseph. -
1:03 - 1:06They are numbered zero, zero, one, and two.
-
1:06 - 1:10So the second thing is one.
So x sub two is indeed Joseph. -
1:12 - 1:17You know, we can pass them in as sequences
to things like max or min or sum. -
1:19 - 1:22And so the maximum of 1, 9, 2 is 9.
-
1:22 - 1:24And we can loop through them.
-
1:24 - 1:26So here is y, it's a tuple.
-
1:26 - 1:33It's 1, 9, 2. And iteration is going to
through the three, three values, right? -
1:33 - 1:36And so it's going to print out 1, 9, 2,
which runs the intended code once for -
1:36 - 1:39each of the values inside the tuple.
-
1:39 - 1:41And so in this respect they're very much
like lists. -
1:42 - 1:47But they're also different than lists in
some real valuable ways. -
1:47 - 1:50Tuples are immutable, and so if you recall
-
1:50 - 1:53when we talked about lists, we compared them to
strings, because -
1:53 - 1:56both lists and strings are a sequence of
elements where -
1:56 - 1:58the first one is zero, one, two, and
et cetera. -
1:59 - 2:01But if we, if we look at a string for
-
2:01 - 2:05example, and we have a three-character
string A, B, C. -
2:05 - 2:09And we want to change the third character,
y sub two, to D, -
2:09 - 2:12it complains and says, no, you can't do that.
-
2:12 - 2:16But you can do it on a list, so if we have
a list 9, 8, 7, and we -
2:16 - 2:18say x sub two is 6, which is the third
-
2:18 - 2:22item, then the third item changes from
7 to 6. -
2:22 - 2:23Okay?
-
2:23 - 2:28So this is mutable.
This is not mutable. -
2:31 - 2:36And tuples are also like not, are not
mutable. -
2:36 - 2:42They're like strings, they're sort of like
lists, in terms of what they can store. -
2:42 - 2:45But they're like strings in the fact that
they can't be changed. -
2:45 - 2:49So here we create a three-tuple,
a three-item tuple, and we try to change -
2:49 - 2:56the third thing from 3 to 0 and it says
you can't do that, not mutable. -
2:56 - 2:56Okay?
-
2:56 - 3:00So, so it's kind of like lists in
the kind of data that we store them, we -
3:00 - 3:03store in them, and it's kind of like
strings in that -
3:03 - 3:05you can't change them once you
create them. -
3:05 - 3:09So this parentheses, this constant, is the
moment of creation. Once -
3:09 - 3:12you've put the things in, you can't fiddle
around with them. -
3:13 - 3:15There's a bunch of other things you can't
do with tuples. -
3:15 - 3:18You think why am I even, why even use
tuples? -
3:18 - 3:19We'll get to that in a second.
-
3:19 - 3:21So here is a three-tuple
-
3:21 - 3:23with the numbers 3, 2, 1.
-
3:23 - 3:27You can't sort it, because if you
sorted it, that would change it. -
3:27 - 3:29You can't add to it.
-
3:29 - 3:32You can't append the value 5 to the end
of it, because that would change it. -
3:32 - 3:36And you can't reverse it.
So, none of these are allowed. -
3:38 - 3:43Those are things you can do with lists,
but you can't do with tuples. -
3:43 - 3:44And you can read the documentation, but we
-
3:44 - 3:47can also use that built-in dir function,
that really -
3:47 - 3:50awesome dir function, where we make a list
and we say hey, Python, -
3:50 - 3:53what will you let me do with lists?
-
3:53 - 3:56Well, you can append, count, extend,
index, -
3:56 - 3:59insert, sort, reverse, remove, pop. Lots
of things. -
3:59 - 4:03Now we make a tuple and say hey, Python,
what can we do with tuple? -
4:03 - 4:05Well, you can do a count or an index,
-
4:05 - 4:07which means you can't do all these other
things. -
4:07 - 4:10So this is sort of a, a very much a
reduction. -
4:13 - 4:15because everything you can do with tuples,
you can do with lists. -
4:15 - 4:18But not everything you can do with lists,
you can do with tuples. -
4:18 - 4:19So why?
-
4:19 - 4:23Why did I just waste all this time
introducing tuples? -
4:23 - 4:24All they are is have parentheses.
-
4:26 - 4:26What good are they?
-
4:26 - 4:29Well, it turns out that they're much more
efficient. -
4:29 - 4:31Because Python doesn't have to deal with
-
4:31 - 4:34the fact that we, as programmers, might
change them, -
4:34 - 4:38Python can make them quicker, they can use
less memory, -
4:38 - 4:42all kinds of things that save a lot of
processing time in Python. -
4:44 - 4:45So when would you use a tuple?
-
4:45 - 4:46Well, in particular, if you're going to
-
4:46 - 4:50create some list that you're never
changing, we prefer to use tuples. -
4:50 - 4:52And there's a lot of situations in
-
4:52 - 4:56programming where we create what we think
of as a temporary variable. -
4:56 - 5:00And if we're going to use, create it, use
it, and throw it -
5:00 - 5:05away without ever modifying it, we prefer
tuples in those kinds of situations. -
5:05 - 5:09Okay? So we prefer tuples when we create
things that are just temporary. -
5:09 - 5:11It's the fact that they're temporary
variables. -
5:11 - 5:14They're like temporary lists. Because
they're efficient. -
5:14 - 5:16They're quick to make and they're quick to
-
5:16 - 5:17get rid of, and they're quick to go
through. -
5:20 - 5:22Now, another really neat thing about
Python that I -
5:22 - 5:25really like is that is the fact that you
can -
5:25 - 5:28do sort of two assignments in one by putting
a tuple on -
5:28 - 5:31both the left and the right-hand side of
your assignment statement. -
5:31 - 5:33So if we think about an assignment
statement, -
5:33 - 5:35I like to think of it as having a direction.
-
5:35 - 5:37It says, these things go there.
-
5:37 - 5:42Well, in Python, you can actually send two
things at the same time. -
5:42 - 5:45The 4 goes into the x and the fred
goes into the y. -
5:45 - 5:47This is a tuple.
This is a tuple. -
5:47 - 5:50You, you cannot have constants on this
left-hand side. -
5:50 - 5:53You can have variables or constants on
the, or expressions -
5:53 - 5:57on the right-hand side, but this must be
two variables. -
5:57 - 6:02Similarly, in this, the 99 goes into a and
the 98 goes into b. -
6:02 - 6:04Now, it turns out that you can
-
6:04 - 6:08syntactically eliminate the parentheses if
you really want. -
6:08 - 6:10And so this leads to a prettier syntax,
-
6:10 - 6:10I think.
-
6:10 - 6:15It's the exact same thing with or without
parentheses, where we basically just say, -
6:15 - 6:21hey, come back, a and b are assigned to
the tuple 99, 98. -
6:21 - 6:23And so you can eliminate the parentheses
as long -
6:23 - 6:26as it's very clear what's going on in the
tuple. -
6:26 - 6:27And so this, this might be a little
disquieting -
6:27 - 6:30when you first see it, but it's just a
-
6:30 - 6:35tuple with no parentheses and the 99 goes
to the a and the 98 goes to the b. -
6:35 - 6:38Now, it turns out we already did this.
-
6:38 - 6:43I sort of blew by this in the previous lecture
in dictionaries -
6:43 - 6:45because it allows us to go through the
-
6:45 - 6:48dictionary's keys and values with two
iteration variables. -
6:51 - 6:53And so, if you remember, here is a
dictionary. -
6:53 - 7:02We put two items into it and and we can
call d.items and get a list -
7:02 - 7:06of tuples, a list of two-tuples.
-
7:06 - 7:10Two-tuples are a quick way of saying a
tuple with two things in it. -
7:10 - 7:15It's a two-element list that consists each
element is a two-tuple. -
7:15 - 7:17And it's the key and the value,
-
7:17 - 7:21key and the value. And so if we
just print this out, it's a list. -
7:21 - 7:26So then when we put this on a for loop, it
-
7:26 - 7:32is a list, but the things inside the list
are each a tuple. -
7:33 - 7:36Each thing inside the list is a tuple.
-
7:37 - 7:41So, when this iteration variable goes to
there, -
7:41 - 7:41it is like
-
7:41 - 7:45this tuple is being assigned into k,v.
Which means the key, -
7:45 - 7:49key goes into k and the value goes
into v. -
7:49 - 7:52The name I picked for k and v don't
matter, do not matter. -
7:53 - 7:55It's just, it's just the first the first
one and the second one. -
7:57 - 8:02So k go, k and v point here.
Then the next time through the loop, k -
8:02 - 8:06and v point here. And so that's
-
8:06 - 8:11how csev 2 and Chen Wen 4 happen.
-
8:11 - 8:15And so this is really a tuple assignment
or a tuple iterating -
8:15 - 8:21through a list of tuple iteration
variable or a pair of iteration variables -
8:21 - 8:23walking through the list, okay?
-
8:25 - 8:29We don't do this a lot, and it's really
quite, it's most heavily -
8:29 - 8:32used for this situation where you're going
through a dictionary and you want to see -
8:32 - 8:34both the keys and the values.
-
8:34 - 8:38And then you use this method inside of
dictionary called d.items. -
8:39 - 8:41Another thing that's cool about tuples
-
8:41 - 8:48are that they're comparable.
So less than, greater than, equals. -
8:48 - 8:55And so, they look, they first compare
the first, leftmost, thing, then -
8:55 - 8:58if that matches, they go to the second
one, and then if that one matches, -
8:58 - 8:59they go to the third one.
-
8:59 - 9:03And so if we're asking, is (0, 1, 2)
less than (5, 1, 2). -
9:03 - 9:04And the answer is True.
-
9:04 - 9:08And it only looks at the 0 and the
5, and that's less than, so away we go. -
9:09 - 9:13If we ask is (0, 1, 2000000) less
than (0, 3, 4)? -
9:13 - 9:17Well, 0 and 0 match, so it goes to
the second one. -
9:17 - 9:201 and 3, well they don't match and
they're less than, -
9:20 - 9:23so 1 is less than 3, so it
-
9:23 - 9:26so it's True and it doesn't even look at
these numbers because it doesn't have to. -
9:26 - 9:26Right?
-
9:26 - 9:28In this one, it doesn't look at those
numbers. -
9:28 - 9:35And now if we say, come here, is Jones,
Sally less than Jones, Fred? -
9:35 - 9:38Well, it compares this and they're equal.
-
9:38 - 9:42So then it has to look to the second one:
is Sally less than Fred? -
9:42 - 9:45Well, no, because S is not less than F.
-
9:45 - 9:51And so that answer is False.
Is Jones, Sally -
9:51 - 9:55greater than Adams, Sam? Well Jones is
greater than Adams, so it -
9:55 - 9:58never looks at these variables, and that
turns out to be True. -
9:59 - 10:02So these are comparable.
-
10:02 - 10:05Which means we can use the less than, less
than or equal to, -
10:05 - 10:09greater than or equal to, equal to, or
not equal to. -
10:09 - 10:13So we can use these operators on whole
tuples. -
10:13 - 10:15Now this turns out to be quite nice,
-
10:15 - 10:20because things that can be compared can
also be sorted. -
10:21 - 10:23Okay?
-
10:23 - 10:24So here is
-
10:24 - 10:28[COUGH] a, b, and c.
a maps to 10. -
10:28 - 10:30b maps to 1.
c maps to 22. -
10:30 - 10:33If I look at d.items,
-
10:33 - 10:37I get back a list of two-tuples, three
two-tuples. -
10:37 - 10:41They are not sorted because dictionaries
-
10:41 - 10:43aren't sorted. a maps to 10,
-
10:43 - 10:44c maps to 22,and b maps to 1.
-
10:44 - 10:49The order that these come out in
is not something that we can control. -
10:49 - 10:52But if we put these items into a variable,
-
10:52 - 10:57call it t, t is the list of tuples
basically, -
10:57 - 11:01and then we tell it to sort, it can do
comparisons between all these. -
11:03 - 11:06And it can sort them and now they're
sorted -
11:06 - 11:08in key order: a, b, c.
-
11:08 - 11:10Now you'll never get any keys that match
-
11:10 - 11:12so it never looks at the second one, right?
-
11:12 - 11:16Because there's one and only one key a or
b or c. -
11:16 - 11:20The value 10 never gets looked at.
So this ends up sort by keys. -
11:20 - 11:28Sort by keys.
Okay, so this is the way to sort by keys. -
11:29 - 11:31We take a dictionary.
-
11:31 - 11:36We get back a list of tuples, key-value
tuples, then we sort that dictionary. -
11:36 - 11:40I mean, sort that list of key-value tuples.
And then, it's sorted by key. -
11:40 - 11:42Okay?
So that's one sort. -
11:43 - 11:46There is a built-in function in Python
-
11:49 - 11:53called sorted, which takes as a
parameter a list, -
11:53 - 11:56and gives you back a sorted version of
that list. -
11:56 - 12:00So we can collapse these operations by
saying, oh, -
12:00 - 12:04well d sub items is this list of tuples
non-sorted. -
12:04 - 12:09But sorted of d sub items is that same
list of tuples, but then sorted. -
12:09 - 12:14So immediately in one step we have
-
12:14 - 12:16a, b, and c properly sorted.
-
12:16 - 12:20And we can combine into all this into one
nice little for -
12:20 - 12:24statement, where we say for k, v in
sorted of d sub items. -
12:24 - 12:29So this is now going to first sort the
key-value pairs by key. -
12:29 - 12:32And then k, v is going to run through them,
so k's -
12:32 - 12:36going to be a, 10. Then it's going to, k's
going to be b, -
12:36 - 12:38v is going to be 1. k is going to be c,
-
12:38 - 12:40v is going to be 22.
-
12:40 - 12:45So now we've printed these things out in
alphabetical key order. -
12:45 - 12:45Okay?
-
12:45 - 12:48So by adding sorted to d.items, that
means that -
12:48 - 12:52this loop is going to run in key-sorted
order. -
12:54 - 12:55Key-sorted order.
-
12:56 - 13:01And that's because sorted takes a list and
then returns, as a, -
13:01 - 13:05takes a list as unsorted list as input and
returns a sorted list. -
13:07 - 13:08Okay?
-
13:10 - 13:15Now, if we are doing something like our
common problem of what's the most common word, -
13:15 - 13:19what if we want to say, what's the five
most common words? -
13:19 - 13:22In that case, we probably want to sort in
-
13:22 - 13:26descending order by the values, not the
key. -
13:27 - 13:27Okay?
-
13:29 - 13:31So we want sort by the values instead of
the key. -
13:32 - 13:37So this is a situation where we're
going to create a temporary variable. -
13:37 - 13:40So here's how we're going to do it.
-
13:40 - 13:44Here is our dictionary with a, 10 and we
want to sort now by -
13:44 - 13:48the values, we want to, you know, maybe see
the most common or sort by the values. -
13:48 - 13:51And so we are going to make a temporary
list -
13:51 - 13:54and then we are going to loop through the
items. -
13:54 - 13:59So, so this is going to just loop through
them and it's -
13:59 - 14:02going to loop through them in non-sorted
order and we are -
14:02 - 14:08going to add using the append operation to
this little list that we are making. -
14:08 - 14:14But we're going to add a tuple that
is value, comma, key. -
14:14 - 14:19So if we make the value first and the key
second in this tuple. -
14:19 - 14:23So this syntax here, this parentheses v
comma k. -
14:23 - 14:27that means make a two-tuple with values
from the v and -
14:27 - 14:28k variable.
-
14:28 - 14:33And, append a list. So you're going to end
up with a list of two-tuples. -
14:35 - 14:40So if we, if we take a look when we're all
done with this, each of these is a tuple. -
14:40 - 14:4610, a gets appended; 22, c gets appended,
and it was simply the opposite order. -
14:46 - 14:51The, the tuple, each of the tuples now
has the value first and the key second. -
14:51 - 14:52Value first, key second.
-
14:52 - 14:54Value first, key second.
-
14:54 - 14:57So this is a bit of temporary data that
-
14:57 - 15:01we've created, this is a bit of temporary
data that we've created. -
15:01 - 15:04Then what we do is we call the sort
method. -
15:04 - 15:10Sort, take this list. Lists are mutable.
The individual tuples can't be changed, but -
15:10 - 15:14the order of the tuples can be changed
because they are in a list. -
15:14 - 15:18tmp.sort, and then we're going to say
reverse equals True -
15:18 - 15:21so you sort from the highest down to the
lowest. Okay? -
15:21 - 15:24And now, tmp has been sorted
-
15:24 - 15:26and now it is in a new order.
-
15:26 - 15:3022, 10, 1 is what caused it to be
sorted. -
15:30 - 15:34So we know that the biggest value is 22,
the key -
15:34 - 15:38of c. Next biggest is 10 with a key of a.
-
15:38 - 15:42And the smallest is a key of 1, a value
of 1 with a key of b. -
15:42 - 15:44So the trick here is
-
15:44 - 15:47if we want to sort in some other
way, we just construct -
15:47 - 15:50a list where we put it in the order that
we want it sorted. -
15:50 - 15:52And this is more important now.
-
15:52 - 15:57The value is more important than the key.
Now if we had another, -
15:57 - 16:02like a 22, f, it would sort first on
the 22. -
16:02 - 16:06And then it would, it would sort the f, 1
after the c, 1. -
16:06 - 16:06Right?
-
16:06 - 16:08So we don't have any duplicates.
-
16:08 - 16:10But we could have the we could have the
key of -
16:10 - 16:13c to 22 and we could have f also be 22.
-
16:15 - 16:19Okay, so, take some time on this, get this
one right. -
16:19 - 16:23So now I want to show you a program
-
16:23 - 16:26that is going to show you the ten most
common words. -
16:26 - 16:28We did a, a loop before
-
16:31 - 16:33where we did the
-
16:34 - 16:38most common word by doing a maximum loop
at the end by looking -
16:38 - 16:42through all of the counts in a dictionary
and then picking the maximum. -
16:42 - 16:44But what if you wanted the top ten?
-
16:44 - 16:46Right, but that, that you don't want to
write -
16:46 - 16:48a loop for that, so we're going to use
sorting. -
16:48 - 16:51So here's what we're going to do.
We're going to open a file. -
16:51 - 16:55We're going to create a empty counts
dictionary. -
16:55 - 16:56Then we're going to
-
16:56 - 17:01write a for loop that reads each
line for line in fhand. -
17:01 - 17:05Then I'm going to split each line into
-
17:05 - 17:09words, based on the spaces, using the dot
split. -
17:09 - 17:13Then I'm going to loop through each word
in each line -
17:13 - 17:18and use our histogram or dictionary
pattern where -
17:18 - 17:21I say counts sub word equals counts dot get
-
17:21 - 17:23word comma zero. That basically says
-
17:23 - 17:25go look in counts.
-
17:25 - 17:27If the word key exists, give me back
-
17:27 - 17:30the value that's in that, otherwise give
me zero. -
17:30 - 17:31So this both
-
17:31 - 17:35creates the new entries and updates old
entries. -
17:35 - 17:38All in one nice simple statement.
-
17:38 - 17:42So at the end of this bit of code
right here -
17:42 - 17:48we are going to have counts with keyword
word-count pairs. -
17:48 - 17:51Okay?
So, this is something we've done before. -
17:51 - 17:55It's just dictionaries, reading, splitting.
-
17:55 - 17:59And then this pattern of how to accumulate
in a dictionary. -
17:59 - 18:03Then what we're going to do is we are going
to make a new list -
18:03 - 18:07called l-s-t and now we're doing this
key-value in the items. -
18:07 - 18:12So this is going to go through the
key-value pairs in this list, which is the -
18:12 - 18:15key-value pairs from the dictionary.
Right? -
18:16 - 18:19But then we are going to create this
temporary list -
18:19 - 18:22of tuples that are val, comma, key.
-
18:22 - 18:28So val is like 20, the; 14, hello;
-
18:29 - 18:33and that's what the list is going to
look like, right? -
18:33 - 18:34It's going to be tuples,
-
18:34 - 18:37but it's going to be the value and then the
-
18:37 - 18:39key, rather than the key and the value.
-
18:39 - 18:47This one here is key, value; this one
here, l-s-t, is value, key. -
18:47 - 18:51Now that we have a list that's value, comma,
key, -
18:53 - 18:56we are just going to sort it because now
it's going to sort based on the first -
18:56 - 18:59thing in that tuple and we're going to
reverse it -
18:59 - 19:02so the biggest values are near the top.
-
19:02 - 19:05And so when we're all done this is going
to be -
19:05 - 19:09a list, except it's going to be sorted
based on the value. -
19:09 - 19:11So that's just one step to sort it.
-
19:11 - 19:15So this is a good example of how we sort
of go through some work, we get a data -
19:15 - 19:17structure, a list, the way we want it and
-
19:17 - 19:19now we can sort of leverage the built-in
sort. -
19:19 - 19:23We had to prepare a list so we could use the
built-in sort. -
19:23 - 19:25We could do this by hand, but it'd be very
difficult. -
19:25 - 19:27But it's easier to say I think
-
19:27 - 19:29I'll make a list, and then I'll sort it.
-
19:29 - 19:30Okay?
-
19:30 - 19:32So I, you know, I made two lists
basically. -
19:32 - 19:37I made the original one, then I made this
one just for the purpose of sorting. -
19:37 - 19:40And now what I am going to do to print out
the top ten -
19:40 - 19:44is I am going to write a for loop val, key.
-
19:44 - 19:48Remember, this list l-s-t is value-key.
-
19:48 - 19:52And I'm going to say for val, key in lst,
using list slicing, -
19:54 - 19:56starting at zero, up to but not including
-
19:56 - 19:59ten, which is indeed is the first ten
items. -
20:00 - 20:07Now I'm going to print out key, value, so
it's going to print out the, 22; -
20:07 - 20:12fred, 16; and so I'm going to first print
the first ten. -
20:12 - 20:16So, this list is in val-key order, the
tuples are val-key order. -
20:16 - 20:19And so I'm going to print it out in
key-val just so that I print out in a way -
20:19 - 20:21that makes the most sense.
-
20:21 - 20:24And so, this is a simple way to do a
-
20:24 - 20:27simple histogram of the occurrence of words
in a file. -
20:29 - 20:34So again, you should know this, you should
know every line. -
20:34 - 20:38You should know every line.
-
20:38 - 20:41Go back, review a couple times, but you
should know, -
20:41 - 20:43you should know the meaning of every line
of this. -
20:43 - 20:46And if you do, that's really good. So
-
20:50 - 20:54as you become more powerful and capable
inside Python, you will -
20:54 - 20:58realize that there are sometimes even
shorter ways of doing things. -
20:58 - 21:02Now, what I'm showing you here is not that
different than what was -
21:02 - 21:06on the previous page, it's just really
dense, but you have to concentrate. -
21:06 - 21:10So if, I want you to understand what's on
that previous page. -
21:10 - 21:11If you don't understand this, don't feel bad.
-
21:11 - 21:15I am going to explain it to you but don't
feel bad if you don't get it. -
21:15 - 21:15Okay?
-
21:15 - 21:16So I'm just going to explain it.
-
21:18 - 21:22If it doesn't feel right to you, go back
and look at the previous page. -
21:22 - 21:23Okay.
-
21:23 - 21:27So here we go.
I am going to have a dictionary. -
21:27 - 21:33And then I'm going to print, in one line,
sorted by value. -
21:33 - 21:37So we'll start from the inside out.
-
21:37 - 21:41So this is a thing called list
comprehension. -
21:41 - 21:44It looks like a list constant because we
start with square brackets. -
21:44 - 21:50But this is a Python syntax that says
construct dynamically -
21:50 - 21:56a list of tuples v, comma, k and I would like
-
21:56 - 22:02you to loop through the items with k and v
taking on the successive values. -
22:02 - 22:06So this is creating that reversed list
where value and key -
22:06 - 22:10are the order of the items in each tuple.
-
22:10 - 22:12And it's going to do that, so this is
going to expand. -
22:12 - 22:14It's sort of like, it goes [SOUND]
expands this, -
22:14 - 22:17it makes a temporary list, right now.
-
22:17 - 22:21Now if you look on the previous slide we
call that thing l-s-t. -
22:21 - 22:27But here we don't even call it l-s-t, and
then once we have the list of tuples -
22:27 - 22:34in value-key order, then we simply take
and pass that into sorted. -
22:34 - 22:36This is a function call,
-
22:36 - 22:41the sorted function. And then, now I'm not
reversing it, but the print statement prints -
22:41 - 22:47out its ascending order of the value
1, 10, 22. -
22:47 - 22:50Okay? So this you can, you can make these
more -
22:50 - 22:54dense once you're a little more
comfortable with what's going on. -
22:54 - 22:58It's sometimes easier to construct
something that seems to have -
22:58 - 23:00steps, where you can put, you know,
you can put -
23:00 - 23:01a debug print here,
-
23:01 - 23:03you can put a debug print here, you can
do a debug print here, -
23:03 - 23:06and you kind of see what's
going on, right? -
23:06 - 23:08Whereas once you really understand this
-
23:08 - 23:12you can, you can write some more dense
Python. -
23:12 - 23:15When you understand this, it's okay.
-
23:15 - 23:15Right?
-
23:15 - 23:18So I'm not saying you're supposed to
understand this, but I just want -
23:18 - 23:21to point out that it's possible to do this
in a tighter fashion. -
23:23 - 23:23So,
-
23:25 - 23:29tuples are like lists except that you
can't change them. -
23:29 - 23:30Right?
-
23:30 - 23:35You can't change lists. And now you can
compare them, you can sort them. -
23:35 - 23:39You can sort lists of tuples.
You can't sort within the tuple itself. -
23:39 - 23:44The two values on the left-hand side of
the assignment statement, we can -
23:45 - 23:49use sorted, and we played with sorting
dictionaries by key and value. -
23:49 - 23:50So,
-
23:52 - 23:54that's kind of the end of this lecture.
-
23:54 - 23:57And and so at this point I just want to
kind of congratulate -
23:57 - 24:02you on making it through the first ten
chapters of the book. -
24:02 - 24:05So I'll, I'll drink a cup of tea to you.
-
24:05 - 24:08Here's your cup of tea, here's my toast to
you. -
24:08 - 24:11in my Slitherin cup.
-
24:11 - 24:16And so it's time for a graduation
ceremony. -
24:16 - 24:17So, I'll give a
-
24:17 - 24:22a little graduation speech here with my
graduation hat on and this is my -
24:22 - 24:27this is my Slitherin wand and so, so the
reason I am congratulating you at -
24:27 - 24:32the end of this chapter is that
at this point, you kind of -
24:34 - 24:39know almost, you know all
the fundamentals of programming. -
24:39 - 24:44Programming really comes down to what's
called algorithms and data structures. -
24:44 - 24:50Sometimes we solve a problem by a clever
series of steps that we put together. -
24:50 - 24:54And sometimes we solve a problem by
creating a clever data structure. -
24:56 - 24:59And so the first few chapters were about
algorithms. Steps, -
24:59 - 25:04loops, functions, very procedural. How you
sort of create these threads of -
25:04 - 25:09stepping and do things a bunch of times or
skip around or whatever. -
25:09 - 25:11And in the last three chapters that
we've covered -
25:11 - 25:15we're talking about data structures. And
programming -
25:15 - 25:19power comes when you combine algorithms
and data structures. -
25:20 - 25:25Now in the next chapters, starting with
Chapter Eleven, regular expressions, -
25:25 - 25:29we're going to learn sort of more clever
ways of doing the same thing. -
25:29 - 25:32So you kind of know how to do a lot of
stuff now. -
25:32 - 25:35From this point forward, you'll see, oh,
boy, that's more clever. -
25:35 - 25:38Or we'll use a database.
Oh, that's more clever. -
25:38 - 25:44But it's not fundamentally different. And
so that's why it's important for you -
25:44 - 25:50to understand, before you leave this
moment, to understand everything -
25:50 - 25:51that we've covered so far.
-
25:51 - 25:56Loops, functions, strings, files,
-
25:56 - 26:02tuples, lists, dictionaries, because they
are kind of the foundation and everything -
26:02 - 26:07else will just kind of be a subtle
refinement/improvement. -
26:07 - 26:10So once you understand that you've kind of
begun, you've -
26:10 - 26:13become a basic programmer and I like, I like
poof! -
26:13 - 26:15Like I, I like
-
26:15 - 26:20magically asperio you and turn you into
Pythonio, something like that. -
26:20 - 26:20Okay.
-
26:20 - 26:23Enough with the Harry Potter reference.
-
26:23 - 26:26Thank you for spending all this time with
me. -
26:26 - 26:28If you've gotten this far, I really
appreciate it. -
26:30 - 26:32And of course it's really just the
beginning but -
26:32 - 26:36I hope that it has been a good beginning.
-
26:36 - 26:36Thank you.
- Title:
- Python for Informatics - Chapter 10 - Tuples
- Description:
-
This is Chapter 10 from Python for Informatics - Exploring Information. From www.pythonlearn.com.
All Lectures: http://www.youtube.com/playlist?list=PLlRFEj9H3Oj4JXIwMwN1_ss1Tk8wZShEJ - Video Language:
- English
- Team:
- Captions Requested
- Duration:
- 26:37
Claude Almansi edited English subtitles for Python for Informatics - Chapter 10 - Tuples |