You know lists and dictionaries. Here are two more containers. A tuple is a row that cannot change, which is just right for values that belong together, such as the two numbers of a point. A set keeps every value only once, answers a yes or no question very fast, and can compare two groups: who is in both clubs, who is in only one. You build both, unpack tuples, return two values from a function, remove repeats, and use the set operations to solve small puzzles. A lesson on choosing between list, tuple, set and dictionary pulls the four together, and a school clubs project uses them all. The practice runs your code on real data and tells you which results are wrong.
5 lessons, about 2 h 35 min5 simulations13 problems10 practice questionsPython in every lesson
A tuple is a row of values written with round brackets that cannot be changed after it is made. It is read by position, like a list, but an assignment to an item gives a TypeError. The commas make a tuple, and a tuple with one item needs a comma. Unpacking puts each item into its own variable, which also swaps two variables. A function that returns two values sends back a tuple. A list of tuples is a good list of small records, and a tuple can be a dictionary key.
A set keeps each value only once, has no order, and so has no positions. set of a list removes the repeats. add puts a value in and does nothing if it is already there, and discard takes a value out without an error. The word in on a set is very fast. len counts the different values, and sorted makes a list in order to show a set neatly. An empty set is set, because the curly brackets alone make an empty dictionary. A set holds only fixed values, so it cannot hold a list.
Two sets can be compared. The sign & gives the values in both sets, the intersection. The sign | gives the values in either, the union. The sign - gives the values only in the first, the difference, and ^ gives the values in exactly one of the two. The same operations exist as methods, and a few tests ask whether one set is inside another, or whether two sets share nothing. Use sets when the questions are about overlap or about different values, and use sorted to show the result.
Python has four containers for many values. A list keeps an order, can change, and allows repeats. A tuple keeps an order, cannot change, and allows repeats. A set has no order, allows no repeats, and gives a fast yes or no test. A dictionary finds a value by its key, and the keys are unique. To choose, ask three questions: do I need an order or a position, do I need to change it, and do I look things up by a name. You can change one container into another with list, tuple, set and dict, and with items.
In this project we manage the clubs of a school. The members of each club are a set, and the clubs are kept in a dictionary. With the set operations the program finds all the students, the students who are in every club, the students who are only in one club, how many clubs each student joins, and the overlap of each pair of clubs. We plan the questions and work out the expected answers by hand, build the program in steps, test the unusual cases such as an empty club, and extend it with a club that members can join and leave.
Log in to join the discussion.
Loading…