0% found this document useful (0 votes)
63 views122 pages

16-Putting It All Together

The document outlines a roadmap for a CS106AP course, covering topics such as data structures, object-oriented programming, and graphics, leading up to a midterm exam. It emphasizes the importance of understanding nested data structures and provides strategies for accessing and manipulating them effectively. Additionally, it discusses mutability in Python, highlighting how mutable and immutable types function within data structures.

Uploaded by

rajeshsv.ignou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views122 pages

16-Putting It All Together

The document outlines a roadmap for a CS106AP course, covering topics such as data structures, object-oriented programming, and graphics, leading up to a midterm exam. It emphasizes the importance of understanding nested data structures and provides strategies for accessing and manipulating them effectively. Additionally, it discusses mutability in Python, highlighting how mutable and immutable types function within data structures.

Uploaded by

rajeshsv.ignou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 122

Putting it all together (pre-midterm)

CS106AP Lecture 16
Roadmap sics
am m i ng Ba
Progr The C
onsol
e Ima
ges
Day 1!

Graphics Data structures


Midterm

Object-Oriented
Everyday Python
Programming

Life after CS106AP!


Roadmap sics
am m i ng Ba
Progr The C
onsol
e Ima
ges
Day 1!

Graphics Data structures


Midterm

Object-Oriented
Everyday Python
Programming

Life after CS106AP!


How do we use data structures in
the real world?

Today’s Who will win the Bluescreen art


show?
questions (How does your computer work? -
Part 1)
1. Review

Today’s 2. Data structures + parsing applications


3. Bluescreen Art Show!
topics 4. (How your computer works) if time
5. What’s next?
A quick note on the midterm
Download BlueBook and the
correct exam beforehand.
(here)

Bring your two-step authentication devices!


Remember...
● We’re not trying to trick you!

● You’ve seen all of this material before and have the tools to solve the
problems.

● We won’t grade you on style.


Common pitfalls
● Mixing up return vs. print()

● Not reading through your code/the provided code line-by-line

● Spending too much time on one problem

● Not showing your work


A good general strategy
● Before getting started, note down/highlight all inputs (parameters) and
outputs (print/return) and their types

● To make sure you understand what you’re being asked to do, walk
through an example input + output for the function/program.

● Draw diagrams (string slicing, program outlines, etc.) to help avoid


bugs (e.g. off-by-one errors, not calling a function, etc.)
Review
Nested data structures
We can nest data structures!
Nested data structures
We can nest data structures!
● Lists in lists X O X
Grid/game board (think TicTacToe!)

X X O

O X O
Nested data structures
We can nest data structures!
● Lists in lists
Grid/game board (think TicTacToe!)
[[X, O, X],

[X, X, O],

[O, X, O]]
Nested data structures
We can nest data structures!
● Lists in lists Feeding times dict
keys (strings) values (lists)
● Lists in dicts ‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

Animals to feeding times ‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Nested data structures
We can nest data structures!
● Lists in lists {'Kylie': {'phone': 6500123445,
'address': '12 Sesame St.'},
'Nick': {'phone': 6501234567,
● Lists in dicts
'address': '34 Clinton Way'},
'Sonja': {'phone': 6502345678,
● Dicts in dicts 'address': '56 Gerard St.'}}
Your phone’s contact book
Nested data structures
We can nest data structures!
● Lists in lists

● Lists in dicts

● Dicts in dicts

● ...and so on!
Accessing nested data
structures
(a formula)
Accessing and manipulating nested data structures
1. What is the outermost data structure?
○ Access its element as normal
Accessing and manipulating nested data structures
1. What is the outermost data structure?
○ Access its element as normal

lst[i] # list: get the value at the index i

d[key] # dictionary: get the value at this key


Accessing and manipulating nested data structures
1. What is the outermost data structure?
○ Access its element as normal

board[row] # get the TicTacToe row

phone_book[name] # get the contact with key name


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!
Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’
Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

This gets the inner list


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

index into the list


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

assign the list element as normal


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

phone_book[‘Kylie’][‘phone’] = 6500123445
Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

phone_book[‘Kylie’][‘phone’] = 6500123445

This gets the inner dictionary


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

phone_book[‘Kylie’][‘phone’] = 6500123445

This gets the value at the key phone


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

phone_book[‘Kylie’][‘phone’] = 6500123445

This sets the value at the key phone


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

board[0][0] = ‘X’

phone_book[‘Kylie’][‘phone’] = 6500123445

Gets the innermost data structure


Accessing and manipulating nested data structures
1. What is the outermost data structure?

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!

You can also call any of the regular list/dict functions on the
innermost data structures
e.g. board[0].append() or phone_book[‘Kylie’].items()
Accessing and manipulating nested data structures
1. What is the outermost data structure? → list/dictionary
○ Access its element as normal

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!
Accessing and manipulating nested data structures
1. What is the outermost data structure? → list/dictionary
○ Access its element as normal

[1.5 Store the element in a variable (optional)]

2. What is the innermost data structure?


○ Work with the element from step 1 like you normally would with
that data structure type!
Accessing and manipulating nested data structures
1. What is the outermost data structure? → list/dictionary
○ Access its element as normal

[1.5 Store the element in a variable (optional)]

first_row = board[0]
first_row[0] = ‘X’ # set upper left corner to ‘X’
# This is the same as board[0][0] = ‘X’
Accessing and manipulating nested data structures
1. What is the outermost data structure? → list/dictionary
○ Access its element as normal

[1.5 Store the element in a variable (optional)]

contact = phone_book[‘Kylie’]
contact[‘phone’] = 6500123445
# This is the same as
# phone_book[‘Kylie’][‘phone’] = 6500123445
Accessing and manipulating nested data structures
1. What is the outermost data structure? → list/dictionary
○ Access its element as normal

[1.5 Store the element in a variable (optional)]

Using an intermediate variable can help make


your code more readable!
Accessing and manipulating nested data structures
1. What is the outermost data structure? → list/dictionary
○ Access its element as normal

[1.5 Store the element in a variable (optional)]

You can call regular list/dict functions on the


variable, too!
Accessing and manipulating nested data structures
1. What is the outermost data structure? → list/dictionary
○ Access its element as normal

[1.5 Store the element in a variable (optional)]

2. What is the innermost data structure?


○ Work with the element/element variable like you would with that
data structure type!
Mutability
Definition
mutable
A Python data type is mutable if the object
can be changed after it is created
Mutability and data structures
● Lists and dicts are both mutable data types
Mutability and data structures
● Lists and dicts are both mutable data types

● Only immutable types can be used as dictionary keys


○ e.g. strings, ints, floats, booleans
Mutability and data structures
● Lists and dicts are both mutable data types

● Only immutable types can be used as dictionary keys

● Immutable or mutable types can be dictionary values


A note on mutability/immutability
Mutable data structures are modified if the variables storing them are
modified...
A note on mutability/immutability
Mutable data structures are modified if the variables storing them are
modified...but not if you reassign that variable!
A note on mutability/immutability
Mutable data structures are modified if the variables storing them are
modified...but not if you reassign that variable!

?????
A note on mutability/immutability
Mutable data structures are modified if the variables storing them are
modified...but not if you reassign that variable!

?????
[demo]
A note on mutability/immutability
Mutable data structures are modified if the variables storing them are
modified...but not if you reassign that variable!

feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

[demo (part 2)]


A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]

‘lumpy’ [‘11’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

feeding_dict[‘lumpy’].append(‘4’)
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]

‘lumpy’ [‘11’, ‘4’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

feeding_dict[‘lumpy’].append(‘4’)
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]

‘lumpy’ [‘11’, ‘4’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

feeding_dict[‘lumpy’] = [‘2’]
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]

‘lumpy’ [‘2’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

feeding_dict[‘lumpy’] = [‘2’]
A note on mutability/immutability
feeding_dict [...]

keys (strings) values (lists)


‘hansa’ [‘12’,‘3’,‘9’] [...]
‘kandula’ [‘8’,‘1’]

‘lumpy’ [‘2’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]
[...]

[...]
Each value in the
A note on mutability/immutability dict is a list object
feeding_dict [...]

keys (strings) values (lists)


‘hansa’ [‘12’,‘3’,‘9’] [...]
‘kandula’ [‘8’,‘1’]

‘lumpy’ [‘2’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]
[...]

[...]
A note on mutability/immutability
feeding_dict [...]

keys (strings) values (lists)


‘hansa’ [‘12’,‘3’,‘9’] [...]
‘kandula’ [‘8’,‘1’]

‘lumpy’ [‘2’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]
[...]

[...]
lump_list = feeding_dict[‘lumpy’]
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]
[‘2’]
‘lumpy’ [‘2’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

lump_list = feeding_dict[‘lumpy’]
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]
[‘2’]
‘lumpy’ [‘2’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

lump_list = feeding_dict[‘lumpy’]
lump_list
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]
[‘2’]
‘lumpy’ [‘2’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

lump_list.append(‘4’)
lump_list
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]
[‘2’, ‘4’]
‘lumpy’ [‘2’, ‘4’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

lump_list.append(‘4’)
lump_list
A note on mutability/immutability
feeding_dict
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]
[‘2’, ‘4’]
‘lumpy’ [‘2’, ‘4’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

lump_list = [‘5’, ‘7’]


lump_list
A note on mutability/immutability Variable
feeding_dict reassignment!
keys (strings) values (lists)
‘hansa’ [‘12’,‘3’,‘9’]

‘kandula’ [‘8’,‘1’]
[‘2’, ‘4’]
‘lumpy’ [‘2’, ‘4’]

‘surus’ [‘5’,‘3’,‘9’,‘2’]

lump_list = [‘5’, ‘7’] lump_list [‘5’, ‘7’]


A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9

‘lumpy’ 3

‘surus’ 4

What if the values aren’t mutable types?


A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9

‘lumpy’ 3

‘surus’ 4

feeding_dict[‘lumpy’] = 4
A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9

‘lumpy’ 4

‘surus’ 4

feeding_dict[‘lumpy’] = 4
A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9

‘lumpy’ 4

‘surus’ 4

feeding_dict[‘lumpy’] += 1
A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9

‘lumpy’ 5

‘surus’ 4

feeding_dict[‘lumpy’] += 1
A note on mutability/immutability
feeding_dict 1

keys (strings) values (ints)


‘hansa’ 1 9
‘kandula’ 9

‘lumpy’ 5

‘surus’ 4
5

4
Each value in the
A note on mutability/immutability dict is a int object
feeding_dict 1

keys (strings) values (ints)


‘hansa’ 1 9
‘kandula’ 9

‘lumpy’ 5

‘surus’ 4
5

4
Each value in the
A note on mutability/immutability dict is a int object
feeding_dict 1

keys (strings) values (ints)


‘hansa’ 1 9
‘kandula’ 9

‘lumpy’ 5

‘surus’ 4
5

4
lump_val = feeding_dict[‘lumpy’]
A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9
5
‘lumpy’ 5

‘surus’ 4

lump_val = feeding_dict[‘lumpy’]
lump_val
A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9
5
‘lumpy’ 5

‘surus’ 4

lump_val += 1
lump_val
Since integers are
A note on mutability/immutability
immutable, a new
feeding_dict Python object gets
keys (strings) values (ints) created!
‘hansa’ 1

‘kandula’ 9
5
‘lumpy’ 5

‘surus’ 4

lump_val
lump_val += 1
A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9
5
‘lumpy’ 5

‘surus’ 4

lump_val
lump_val += 1

(This is the same as lump_val = lump_val + 1)


A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9
5
‘lumpy’ 5

‘surus’ 4

lump_val
lump_val += 1
5+1
5
(This is the same as lump_val = lump_val + 1)
A note on mutability/immutability
feeding_dict
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9
5
‘lumpy’ 5

‘surus’ 4

lump_val
lump_val += 1
6
5
(This is the same as lump_val = lump_val + 1)
A note on mutability/immutability feeding_dict
feeding_dict
doesn’t change!
keys (strings) values (ints)
‘hansa’ 1

‘kandula’ 9
5
‘lumpy’ 5

‘surus’ 4

lump_val
lump_val += 1
6
5
(This is the same as lump_val = lump_val + 1)
A note on mutability/immutability
Mutable data structures are modified if the variables storing them are
modified...but not if you reassign that variable!
A note on mutability/immutability
Mutable data structures are modified if the variables storing them are
modified...but not if you reassign that variable!
A note on mutability/immutability
Mutable data structures are modified if you use a variable to modify the
data structure object itself...but not if you reassign that variable to a new
data structure object!
A note on mutability/immutability
Mutable data structures are modified if you use a variable to modify the
data structure object itself...but not if you reassign that variable to a new
data structure object!

This means that if you pass a data structure into a


function, modifying the data structure within the function
modifies the original data structure!
[demo]
A note on mutability/immutability
Mutable data structures are modified if you use a variable to modify the
data structure object itself...but not if you reassign that variable to a new
data structure object!

This means that if you pass a data structure into a


function, modifying the data structure within the function
modifies the original data structure!
(this matters for Assignment 4)
How do we use data structures
for real-world problems?
How do we use data structures
for real-world problems?
Two main questions!
1. What questions do we want to
answer with the data?

2. Based on what we want to do, what


data structures should we use to
store it?
Potential datasets
● All the students in CS106AP and the classes they’re currently taking

What questions do you want to answer with the data?


Potential datasets
● All the students in CS106AP and the classes they’re currently taking

What questions do you want to answer with the data?


● What classes is an individual student taking?
● How many students are taking both Engr 40A and CS106AP?
● What times are different students’ classes at?
Potential datasets
● All the students in CS106AP and the classes they’re currently taking

What data structures should we use to store it?


● What classes is an individual student taking?
● How many students are taking both Engr 40A and CS106AP?
● What times are different students’ classes at?
Potential datasets
● All the students in CS106AP and the classes they’re currently taking

What data structures should we use to store it?


● What classes is an individual student taking?
○ Dictionary of lists (student names to class lists)
● How many students are taking both Engr 40A and CS106AP?
● What times are different students’ classes at?
Potential datasets
● All the students in CS106AP and the classes they’re currently taking

What data structures should we use to store it?


● What classes is an individual student taking?
○ Dictionary of lists (student names to class lists)
● How many students are taking both Engr 40A and CS106AP?
○ Two lists (one list for Engr40A and one for CS106AP)
● What times are different students’ classes at?
Potential datasets
● All the students in CS106AP and the classes they’re currently taking

What data structures should we use to store it?


● What classes is an individual student taking?
○ Dictionary of lists (student names to class lists)
● How many students are taking both Engr 40A and CS106AP?
○ Two lists (one list for Engr40A and one for CS106AP)
● What times are different students’ classes at?
○ Dictionary of dicts (student names to a dictionary of
times to classes)
Think/Pair/Share:
1. What questions do we want to
answer with the data?
2. What data structures should we use
to store it?
Potential datasets
● All the students in CS106A and the classes they’re currently taking

● Tweets after a natural disaster from people requesting help

● Images of different coral reefs, some with coral bleaching and some without

● Government data on baby names for babies born per year


Potential datasets
● All the students in CS106A and the classes they’re currently taking

● Tweets after a natural disaster from people requesting help

● Images of different coral reefs, some with coral bleaching and some without

● Government data on baby names for babies born per year

1. What questions do you want to answer with the data?


2. What data structures should we use to store it?
Potential datasets
● All the students in CS106A and the classes they’re currently taking

● Tweets after a natural disaster from people requesting help

● Images of different coral reefs, some with coral bleaching and some without

● Government data on baby names for babies born per year


Assignment 4: BabyNames
Parsing wrap-up
Three final functions for parsing
● f.readlines()

● lst.reverse()

● d.get()
Three final functions for parsing
● f.readlines()

● lst.reverse()

● d.get()
Another file function: f.readlines()
0 The suns are able to fall and rise:
1 When that brief light has fallen for us,
2 we must sleep a never ending night.

with open(‘catullus.txt’, ‘r’) as f:


lines = f.readlines()
Another file function: f.readlines()
0 The suns are able to fall and rise:
1 When that brief light has fallen for us,
2 we must sleep a never ending night.

with open(‘catullus.txt’, ‘r’) as f:


lines = f.readlines()

this is a list of strings!


Another file function: f.readlines()
0 The suns are able to fall and rise:
1 When that brief light has fallen for us,
2 we must sleep a never ending night.

with open(‘catullus.txt’, ‘r’) as f:


[‘The suns...’,
lines = f.readlines()
‘When that…’,
‘we must…’]
lines
Three final functions for parsing
● f.readlines()

● lst.reverse()

● d.get()
Another list function: lst.reverse()
>>> lst = [1, 2, 3, 4, 5]
Another list function: lst.reverse()
>>> lst = [1, 2, 3, 4, 5]
>>> lst.reverse()
Another list function: lst.reverse()
>>> lst = [1, 2, 3, 4, 5]
>>> lst.reverse()
>>> lst
Another list function: lst.reverse()
>>> lst = [1, 2, 3, 4, 5]
>>> lst.reverse()
>>> lst
[5, 4, 3, 2, 1]
Another list function: lst.reverse()
>>> lst = [1, 2, 3, 4, 5]
>>> lst.reverse()
>>> lst
[5, 4, 3, 2, 1]
This is different from [::-1] since it
changes the original list!
Three final functions for parsing
● f.readlines()

● lst.reverse()

● d.get()
Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

d.get(key, default)
Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

d.get(key, default)

If the key doesn’t exist, it will return


the default value instead.
Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

d.get(key)

The default parameter is optional.


If left out, d.get() will return None
if the key doesn’t exist.
(i.e. default defaults to None)
Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

>>> d = {‘a’: 3, ‘b’: 3, ‘c’: 1}


Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

>>> d = {‘a’: 3, ‘b’: 3, ‘c’: 1}


>>> d.get(‘a’, 0)
Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

>>> d = {‘a’: 3, ‘b’: 3, ‘c’: 1}


>>> d.get(‘a’, 0)
3
Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

>>> d = {‘a’: 3, ‘b’: 3, ‘c’: 1}


>>> d.get(‘a’, 0)
3
>>> d.get(‘d’, 0)
Another dict function: d.get()
It’s similar to d[key], but helps us avoid a KeyError if the key doesn’t exist!

>>> d = {‘a’: 3, ‘b’: 3, ‘c’: 1}


>>> d.get(‘a’, 0)
3
>>> d.get(‘d’, 0)
0
Bluescreen Art Show!
(Vote here)
What’s next?
You’ve learned so much!
● Karel ● Strings
● Control flow ● Console programs
○ Conditionals: if, if-else, ● Images
if-elif-else statements ● Parsing
○ Loops: while, for-each, ● File reading
for-range ● Data structures
● Functions and decomposition ○ Lists
● Variables and constants ○ Dictionaries
● Basic data types (integers, ○ Nested data structures
floats, booleans) ● Style best practices
Roadmap sics
am m i ng Ba
Progr The C
onsol
e Ima
ges
Day 1!

Graphics Data structures


Midterm
Graphics 1.0
Object-Oriented
Graphics 2.0
Everyday Python
Event-drivenProgramming
programming

Life after CS106AP!


Roadmap sics
am m i ng Ba
Progr The C
onsol
e Ima
ges
Day 1!

Graphics Data structures


Midterm
Graphics 1.0
Object-Oriented
Graphics 2.0
Everyday Python
Event-drivenProgramming
programming

Life after CS106AP!

You might also like