Python · Module 3
Data Structures6 lessons and their Study toolkit.
Watch each lesson, answer its quiz, then do its practice. After the last lesson: the module quiz, the project, and the interview questions.
6of 6 lessons ready
45quiz questions
30key terms
4self-checking notebooks
The lessonsin the order to take them.
6 lessons ready to study
- 3.1ListsStore ordered, changeable collections and slice them. Open
- 3.2TuplesUse fixed-size immutable records, and unpack them. Open
- 3.3SetsKeep unique items and ask membership questions fast. Open
- 3.4DictionariesMap keys to values: counting, grouping, lookup. Open
- 3.5StringsClean and split text, the first step of every text model. Open
- 3.6ComprehensionsBuild lists, sets and dicts in one readable line. Open
Glossaryevery term the module introduces.
Each with the lesson that introduces it.
- alias
- A second name bound to the same object: after
backup = cart,backupis an alias ofcart. 3.1 - element
- One value stored in a list. 3.1
- index
- An element's position, counting from zero. A negative index counts from the end, from
-1. 3.1 - method
- A function that belongs to an object, called with a dot:
cart.append(...),review.lower(). 3.1 - mutable
- A type whose objects can be changed after they are created; a list is mutable, and so are sets and dictionaries. 3.1
- mutation
- Changing an object in place, which every alias sees:
append,remove,sort, assigning to an index. 3.1 - slice
- A range of elements, from a start up to but not including a stop, with an optional step:
temps[2:5],temps[::-1]. A slice is a new list. 3.1 - immutable
- An object that can not be changed after it is created: a tuple, a string, a number. 3.2
- packing
- Collecting several values, separated by commas, into one tuple:
point = 12.9716, 77.5946. 3.2 - tuple
- An ordered sequence of values, written with commas, that can not be changed:
(12.9716, 77.5946). 3.2 - unpacking
- Binding each item of a sequence to its own name, in order:
lat, lon = point. 3.2 - difference
- The values in the first set that are not in the second,
a - b. 3.3 - hash
- A number Python computes from a value, which a set uses to decide where the value is stored. A dictionary uses its keys' hashes in the same way. 3.3
- hashable
- A value whose hash never changes, so it can be a set member or a dictionary key: numbers, strings, and tuples of hashable values. Lists, sets and dictionaries are not. 3.3
- intersection
- Every value in both sets,
a & b. 3.3 - set
- An unordered collection in which each value appears at most once. An empty set is
set(). 3.3 - Counter
- A dictionary that counts; a missing key counts zero.
Counter(votes).most_common(1)gives[('tea', 3)]. Fromcollections. 3.4 - defaultdict
- A dictionary that creates a missing key's value when the key is read:
defaultdict(list)creates[]. Fromcollections. 3.4 - dictionary
- A collection of key value pairs, looked up by key:
{"Pasta": 12.50, "Salad": 8.25}. Its type isdict. 3.4 - key
- What a dictionary looks a value up by, such as
"Pasta"in the menu. Each key appears once, and must be hashable. 3.4 - key value pair
- One entry of a dictionary: a key and its value, written
"Pasta": 12.50. 3.4 - nested dictionary
- A dictionary whose values are dictionaries:
cafe["drinks"]["Juice"]. 3.4 - value
- What a dictionary stores for each key, such as
12.50for"Pasta". Values can be anything, including lists and dictionaries. 3.4 - chaining
- Calling a method on the value the previous method returned:
review.strip().lower(). 3.5 - string
- An immutable sequence of characters, indexed and sliced like a list. 3.5
- token
- One of the pieces tokenisation produces. Our tokeniser's tokens are lowercase words without punctuation. 3.5
- tokenisation
- Splitting text into pieces. Language models split into subword pieces; the video's tokeniser splits on whitespace. 3.5
- dictionary comprehension
- A dictionary built from a key and a value evaluated for each item:
{item: price for item, price in zip(items, prices)}. 3.6 - list comprehension
- A list built from an expression evaluated for each item of an iterable:
[round(p * 1.08, 2) for p in prices], with an optionalifat the end to filter. 3.6 - set comprehension
- A set built from an expression evaluated for each item:
{len(item) for item in items}. 3.6
Module quiz15 questions across it all.
Take it after the last lesson. Your first pick on each question is the one that counts.
Project: The Most Common Words in Product Reviewsbuild it without a template.
Stated as a problem, with no step-by-step instructions. Working out the steps is the point.
The task
Write a program, in one Colab notebook, that does the following.
04_module_project.ipynb checks the core of it. Your program must:
- clean every review the way 3.5's tokeniser does: lowercase, with
.,,,!and?removed, then split into words, all in one list; - print how many words there are, and how many different words;
- count every word with a dictionary you build yourself, then check that the
counts are equal to
Counter's; - print the five most common words with their counts, as aligned rows, using
most_common(sorting a dictionary by its values needskey=, which is Python 4.4); - remove every word in
STOP_WORDS, and print the five most common words again.
Notebooksthat check your answers.
Open them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
- Notebook 1 · Lists and TuplesLessons 3.1 and 3.2 Courses plan
- Notebook 2 · Sets and DictionariesLessons 3.3 and 3.4 Courses plan
- Notebook 3 · Strings and ComprehensionsLessons 3.5 and 3.6 Courses plan
- Notebook 4 · Project: The Most Common Words in ReviewsThe module project, with checks Courses plan
Referencefor revising and for interviews.
The cheat sheet is one page of the module’s terms, rules and gotchas. The interview questions come with model answers.