Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Midterm Tips and Dictionaries 2.0
CS106AP Lecture 14
RoadmapProgramming Basics
The Console Images
Data structures
MidtermGraphics
Object-Oriented Programming
Everyday Python
Life after CS106AP!
Day 1!
Images
The Console
Everyday PythonObject-Oriented Programming
MidtermGraphics
Programming Basics
Roadmap
Life after CS106AP!
Day 1!
Data structures
ListsFilesParsing: Strings
Dictionaries 1.0
Dictionaries 2.0
Nested Data Structures
Today’s questions
How can I effectively prepare for the midterm?
How can I organize and examine my data?
Today’s topics
1. How to Prepare for the Midterm
2. Review
3. Organizing Data
Dictionaries
Built-in Functions
4. What’s next?
Midterm Preparation
Midterm Logistics
● Monday, July 22 7:00-9:00pm at Hewlett 200
○ Please arrive 5-10 minutes early
● If you have an alternate exam, Nick will be in contact about time and
location
● Bring your own laptop! (+ charger)
Midterm Logistics
● Closed-internet
● You are allowed 10 pages (double-sided) of notes
● We will provide a reference sheet of common functions for strings,
lists, dicts, etc.
○ We will publish it with the practice midterm
Midterm Logistics
● You will need to download BlueBook, which is our test-taking software
○ Linked on our home page
● You should test out BlueBook beforehand!
Midterm Practice
● We will publish the practice midterm tomorrow
● Nick will hold a midterm review session Friday 1:30-2:30pm in Gates B1
○ He will recap course content!
Midterm Content
● Material is from Karel to what we cover in lecture today
○ (Wednesday, July 17, Lecture 14)
● Topics: Karel, console programs, strings, images, parsing,
lists, files, dicts
● Types of problems: trace, coding functions
● No Doctests required!
How do I study for the midterm?
● Content Review
○ lecture slides, videos
● Content Application
○ practice exam, section problems, lecture examples
How do I study for the midterm?
● Content Review
○ lecture slides, videos
● Content Application
○ practice exam, section problems, lecture examples
Content Application > Content Review!
Midterm Studying Tips & Tricks
● Practice problems without looking at the solutions
○ practice exam (time yourself!), section problems, lecture examples
○ “I could have figured that out.” Be the change you want to see!
● Creating your notes as a way of reviewing concepts
○ writing by hand can help you remember!
○ summarize materials in your own words or quiz yourself!
If you’re studying and get stuck/get a problem wrong...
● Read over the solution
● Then, come back later and solve the problem again without looking at
the solution
○ “Reps” (repetitions) can help you learn!
If you’re stuck when solving a problem...
If you’re stuck when solving a problem...
● Make a drawing
If you’re stuck when solving a problem...
● Make a drawing
● Write down the inputs and outputs
If you’re stuck when solving a problem...
● Make a drawing
● Write down the inputs and outputs
● Describe in your own words what task you want to accomplish
If you’re stuck when solving a problem...
● Make a drawing
● Write down the inputs and outputs
● Describe in your own words what task you want to accomplish
○ Then use top-down decomposition and/or pseudocode
If you’re stuck when solving a problem...
● Make a drawing
● Write down the inputs and outputs
● Describe in your own words what task you want to accomplish
○ Then use top-down decomposition and/or pseudocode
● Look over the reference sheet and think about what concepts might
help you accomplish that task!
Midterm Strategy
● We don’t mark you down for syntax errors (e.g. typos, missing ‘:’)
unless we can’t tell what you meant.
● We want to give you partial credit!
○ Show your work and describe your thought process even if you’re
not 100% sure
Review
DictionaryA container data type that maps “keys” to their
associated “values”.
Definition
Adapted from Jon Skeet
Anatomy of a Dictionary - Get/Set
>>> d[‘hansa’]
4
this operation is called “get”
dict
‘hansa’‘kandula’‘lumpy’‘surus’
4326
keys values
Anatomy of a Dictionary - Get/Set
>>> d[‘hansa’]
4
>>> d[‘hansa’] = 5
this operation is called “set”
dict
‘hansa’‘kandula’‘lumpy’‘surus’
5326
keys values
Anatomy of a Dictionary - Get/Set
>>> d[‘hansa’]
4
>>> d[‘hansa’] = 5
Note: get and set are fast!
dict
‘hansa’‘kandula’‘lumpy’‘surus’
5326
keys values
Dictionaries + in
>>> ‘hansa’ in d
True
>>> ‘nick’ not in d
True
dict
‘hansa’‘kandula’‘lumpy’‘surus’
5326
keys values
Common pattern:Check if key is present. If it is, do something. If it isn’t, do something else.
Building a Dictionary
>>> d = {}
>>> d[‘hansa’] = 3
>>> d
{‘hansa’: 3}we can add keys using “set”!
create an empty dictionary
Building a Dictionary
>>> d = {‘hansa’: 3}
>>> d[‘hansa’] += 2
>>> d
{‘hansa’: 5}we can get/set on the same line!(same as d[‘hansa’] = d[‘hansa’] + 2)
Summary
>>> d = {} # new dict
>>> d[‘hansa’] = 2 # set
>>> d[‘hansa’] # get
2
>>> ‘hansa’ in d # check membership
True
Think/Pair/Share:Write a program that counts the number of times each word occurs in a file and outputs a word to word_count dict.
Input: filename
How can I organize and examine my data?
Types of Dictionaries
● So far, we’ve seen dictionaries mapping from strings to ints
Types of Dictionaries
● So far, we’ve seen dictionaries mapping from strings to ints
○ This is not the only type of dictionary!
Types of Dictionaries
● So far, we’ve seen dictionaries mapping from strings to ints
○ This is not the only type of dictionary!
○ You can map from string/int/float to string/int/float...
Types of Dictionaries
● So far, we’ve seen dictionaries mapping from strings to ints
○ This is not the only type of dictionary!
○ You can map from string/int/float to string/int/float...
■ ...and more! (coming tomorrow)
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> d.keys()
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> d.keys()
dict_keys(['Gates', 'MemChu', 'Tresidder'])
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> d.keys()
dict_keys(['Gates', 'MemChu', 'Tresidder'])
iterable collection of all the keys.iterable means it can be used in foreach
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> list(d.keys())
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> list(d.keys())
[‘Gates’, ‘MemChu’, ‘Tresidder’]
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> list(d.keys())
[‘Gates’, ‘MemChu’, ‘Tresidder’]
we are using list() to convert d.keys() into a list
Accessing a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
Accessing a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> list(d.values())
Accessing a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> list(d.values()) we are using list() to convert d.values() into a list
Accessing a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> list(d.values())
[23, 116, 57]
we are using list() to convert d.values() into a list
Looping over a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
Looping over a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building in d.keys():
Looping over a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building in d.keys():
... print(building)
Looping over a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building in d.keys():
... print(building)
Gates
MemChu
Tresidder
Looping over a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building in d.keys():
... print(building)
Gates
MemChu
Tresidder
we can use foreach on the dictionary’s keys!
Looping over a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
Looping over a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for age in d.values():
Looping over a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for age in d.values():
... print(age)
Looping over a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for age in d.values():
... print(age)
23
116
57
Looping over a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for age in d.values():
... print(age)
23
116
57
we can use foreach on the dictionary’s values!
Looping over a Dictionary’s Keys and Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
Looping over a Dictionary’s Keys and Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
Looping over a Dictionary’s Keys and Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():items() gives us key, value pairs
Looping over a Dictionary’s Keys and Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, ‘is’, age, ‘years old.’)
items() gives us key, value pairs
Looping over a Dictionary’s Keys and Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, ‘is’, age, ‘years old.’)
Gates is 23 years old.
MemChu is 116 years old.
Tresidder is 57 years old.
items() gives us key, value pairs
Looping over a Dictionary’s Keys and Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, ‘is’, age, ‘years old.’)
Gates is 23 years old.
MemChu is 116 years old.
Tresidder is 57 years old.
print() will automatically concatenate args separated by commas!
Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, age, sep=‘: ’)
Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, age, sep=‘: ’)
sep is an optional argument like end!
Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, age, sep=‘: ’)
Gates: 23
MemChu: 116
Tresidder: 57
sep is an optional argument like end!
Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, age, sep=‘: ’)
Gates: 23
MemChu: 116
Tresidder: 57
the separating string will be printed between the arguments you pass into print()
Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> for building, age in d.items():
... print(building, age, sep=‘: ’)
Gates: 23
MemChu: 116
Tresidder: 57
the default is sep=‘ ’(insert space)
Getting a Sorted List of Keys
>>>d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}
Getting a Sorted List of Keys
>>> d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}
>>> sorted(d.keys())
Getting a Sorted List of Keys
>>> d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}
>>> sorted(d.keys())
['Gates', 'MemChu', 'Tresidder']
Getting a Sorted List of Keys
>>> d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}
>>> sorted(d.keys())
['Gates', 'MemChu', 'Tresidder']
sorted() returns a list in alphabetical order!
Getting a Sorted List of Keys
>>> d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}
>>> sorted(d.keys())
['Gates', 'MemChu', 'Tresidder']
>>> d
Getting a Sorted List of Keys
>>> d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}
>>> sorted(d.keys())
['Gates', 'MemChu', 'Tresidder']
>>> d
{‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}
Sorting a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
Sorting a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> sorted(d.values())
Sorting a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> sorted(d.values())
[23, 57, 116]
Sorting a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> sorted(d.values())
[23, 57, 116]
sorted() returns a list in numerical order!
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> min(d.values())
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> min(d.values())
returns the smallest element!
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> min(d.values())
23 returns the smallest element!
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> min(d.values())
23
>>> max(d.values())
returns the smallest element!
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> min(d.values())
23
>>> max(d.values())
returns the smallest element!
returns the biggest element!
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
>>> min(d.values())
23
>>> max(d.values())
116
returns the smallest element!
returns the biggest element!
Built-in FunctionA function built into Python that is always
available for use.
Definition
Examples of Built-ins
print()input()str()int()float()len()
open()list()sorted()max()min()
Examples of Built-ins
print()input()str()int()float()len()
open()list()sorted()max()min()
What’s next?
What’s next?
dict
‘hansa’‘kandula’‘lumpy’‘surus’
keys values
Image from Allie Brosh
Images
The Console
Everyday PythonObject-Oriented Programming
MidtermGraphics
Programming Basics
Roadmap
Life after CS106AP!
Day 1!
Data structures
ListsFilesParsing: Strings
Dictionaries 1.0
Dictionaries 2.0
Nested Data Structures