Topic outline
-
Python's power lies not only in the vast set of modules and libraries available (such as matplotlib, random, math, numpy, etc.), but also in the data structures that are fundamental to the language. This unit introduces three more ways of structuring data that must be mastered: tuples, sets, and dictionaries. We will also revisit the concepts of mutability and immutability, as we saw for lists and strings.
Completing this unit should take you approximately 3 hours.
-
-
We have introduced the concepts of mutable and immutable objects in Python. Recall that lists are mutable while strings are immutable. This section introduces two more immutable data types useful for holding collections of objects: tuples and sets.
In short, tuples are immutable lists. Similar to strings, they cannot be changed once they have been assigned. Everything that you have learned so far about indexing and slicing also applies to tuples. The syntax for forming a tuple uses parentheses (instead of square brackets for forming a list):
a=[23.7, 45, 78] b='asdfasdf' c=(4,9,a,b,'zxcv') print(c) print(type(c))
Notice that we can put any object we please into the tuple defined as the variable
c
. The output after running this code looks almost exactly like a list. However, because of the immutability ofc
, attempting to modify an element with an instruction such asc[1]=99
will result in an error. One obvious use for tuples for holding data that must be write-protected. We will see other uses when we encounter dictionaries.Sets are another immutable data type that holds a unique collection of objects. Sets can be used to perform typical set operations such as union, intersection, set differences, and so on. Duplicates values are not possible within a set. Left and right squirrely brackets are used to assign sets.
a={3,4,5,6,7,9} print(a) print(type(a)) b={3,3,3,4,4,4,5,5,6,7,8} print(b) print(type(b))
You should observe that both
b
andc
contain the same set of objects. The next set of commands should demonstrate Python's ability to perform set operations.a={3,4,5} b={4,5,6,7,8,9} c=a.intersection(b) print(c) d=a.union(b) print(d) e=8 print(e in b) print(e in a) f=b.difference(a) print(f)
This code introduces fundamental operations such as set membership (using the
in
keyword) and a suite of set method calls for computing the union, intersection, and so on.
-
-
-
-
One major application of dictionaries is to be able to efficiently sequence through a set of values. Read this section to practice writing loops using dictionaries.
-
Another application of tuples is to use them as keys in dictionaries. Follow and practice the examples presented in this section in order to understand how tuples can be used with dictionaries.
-
-
We have introduced several basic Python data structures: lists, strings, sets, tuples and dictionaries. Take some time to review, compare and contrast these constructs for handling various kinds of collections.
-
-
-
In this video, course designer Eric Sakk walks through the major topics we covered in Unit 6. As you watch, work through the exercises to try them out yourself.
-
Take this assessment to see how well you understood this unit.
- This assessment does not count towards your grade. It is just for practice!
- You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
- You can take this assessment as many times as you want, whenever you want.
-