Python for Informatics – Chapter 10 – Tuples



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

24 Comments

  1. Heoi Lien said:

    Thank you so much.

    June 28, 2019
    Reply
  2. Dominique Coleman said:

    Your understanding of Python might be one of the best I have seen so far on YouTube. I would love to have you for a mentor but I am sure I can't afford you lol I will probably watch all your videos at least 3 times by the time I am done with learning Python.

    June 28, 2019
    Reply
  3. srinivasa reddy said:

    Mr.Chuck, the class is great. I like the way you present and explains things easily and clearly.

    June 28, 2019
    Reply
  4. Arushi Sharma said:

    Tuple does not have a sort built- in function. Then how can we use sorting of tuples to sort dictionaries?

    June 28, 2019
    Reply
  5. Sudhir singh shekhawat said:

    Thank you sir, Well explained specially Hardware part is awesome

    June 28, 2019
    Reply
  6. FG787 said:

    Hallo

    June 28, 2019
    Reply
  7. Aug said:

    Hi! Very interesting! I always wished I was more mathematical: I'd have loved to have picked up a form of coding or programming.

    June 28, 2019
    Reply
  8. kagnavy Sim said:

    You are super great, professor.

    June 28, 2019
    Reply
  9. maheshwar gurav said:

    2000000 is not less than 4 so how ans is still true?

    June 28, 2019
    Reply
  10. jk jks said:

    Thank you for your effort. Can i ask you a question?can one be a expert in this discipline studying alone, without university? or the study of maths is obligatory?

    June 28, 2019
    Reply
  11. LousyPainter said:

    You can tell when someone really understands a topic when they can explain something clear enough for just about anyone to understand.

    Great work! Fantastic compilation of videos and documentation a.k.a the book 🙂

    Thanks
    John

    June 28, 2019
    Reply
  12. 王紫凡 said:

    Thank you so much! I wish I’d known about this course earlier! It's really fantastic and so helpful!!

    June 28, 2019
    Reply
  13. Hemant S said:

    The entire course was awesome. I am going to do the other chapters as well. Is there a next level book you are able to recommend considering that someone started learning Python only through your Tuts in www.pythonlearn.com . I would like to go deeper in to informatics and data analysis. Thx.

    Also, this is a Py v2 code… the sort() func does not seem to work on the list of tuples returned by d.item(). Is there a Py.v3 fix for it ? thx

    June 28, 2019
    Reply
  14. Ying said:

    Amazing class, thank you very much Chuck. I really learned a lot.

    June 28, 2019
    Reply
  15. revernance said:

    This is a great class. Thanks Dr. Severance. Thanks to all the TAs, and the rest of the team!

    June 28, 2019
    Reply
  16. Robert100R said:

    I appreciate your effort and thoughts regarding free education… 🙂 we need more people like u in this planet before the inhabitants roasts us for the dinner😦

    June 28, 2019
    Reply
  17. Yubin Ruan said:

    I love this guy!

    June 28, 2019
    Reply
  18. Al C said:

    Pedro: If you go to the interpreter and enter ord ('J')  and ord ('A') you'll discover J has a value of 74 (1001010) and A has a value of 65 (1000001). Therefore, Jones is greater than Adams. All keyboard characters have a value so the computer knows what the hell the input is…If memory serves me correctly

    June 28, 2019
    Reply
  19. blackbusiness7 said:

    # Curious: In your word count program, why not
    counts[word] = counts.get(word, 1) # Rather than counts.get(word, 0) + 1?

    June 28, 2019
    Reply
  20. Paul said:

    Thanks for doing this Dr. Chuck. Great tutorial course.

    Anyway you can fix the Chapter 11 Discussion grader? It's currently impossible to get credit. I'm one completion shy of my Blue Ribbon.

    June 28, 2019
    Reply
  21. Kriste Muru said:

    Well explained and fun to watch. Book is also easy to read and understand. Thank you for videos and the book which helped a lot 🙂 

    June 28, 2019
    Reply
  22. luis alfaro said:

    exc! thank you 🙂

    June 28, 2019
    Reply
  23. pedro miguel said:

    Thank you, noob question: why Jones is greater than Adams? 9:55

    June 28, 2019
    Reply
  24. Frank Pérez said:

    Great ! Thank you very much! I'll read the whole book ! (Y)

    June 28, 2019
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *