16-Putting It All Together
16-Putting It All Together
CS106AP Lecture 16
Roadmap sics
am m i ng Ba
Progr The C
onsol
e Ima
ges
Day 1!
Object-Oriented
Everyday Python
Programming
Object-Oriented
Everyday Python
Programming
● You’ve seen all of this material before and have the tools to solve the
problems.
● To make sure you understand what you’re being asked to do, walk
through an example input + output for the function/program.
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’]
‘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
board[0][0] = ‘X’
Accessing and manipulating nested data structures
1. What is the outermost data structure?
board[0][0] = ‘X’
board[0][0] = ‘X’
board[0][0] = ‘X’
board[0][0] = ‘X’
phone_book[‘Kylie’][‘phone’] = 6500123445
Accessing and manipulating nested data structures
1. What is the outermost data structure?
board[0][0] = ‘X’
phone_book[‘Kylie’][‘phone’] = 6500123445
board[0][0] = ‘X’
phone_book[‘Kylie’][‘phone’] = 6500123445
board[0][0] = ‘X’
phone_book[‘Kylie’][‘phone’] = 6500123445
board[0][0] = ‘X’
phone_book[‘Kylie’][‘phone’] = 6500123445
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
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
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
?????
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’]
‘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’]
‘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’]
‘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 [...]
‘lumpy’ [‘2’]
‘surus’ [‘5’,‘3’,‘9’,‘2’]
[...]
[...]
Each value in the
A note on mutability/immutability dict is a list object
feeding_dict [...]
‘lumpy’ [‘2’]
‘surus’ [‘5’,‘3’,‘9’,‘2’]
[...]
[...]
A note on mutability/immutability
feeding_dict [...]
‘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’]
‘kandula’ [‘8’,‘1’]
[‘2’, ‘4’]
‘lumpy’ [‘2’, ‘4’]
‘surus’ [‘5’,‘3’,‘9’,‘2’]
‘kandula’ 9
‘lumpy’ 3
‘surus’ 4
‘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
‘lumpy’ 5
‘surus’ 4
5
4
Each value in the
A note on mutability/immutability dict is a int object
feeding_dict 1
‘lumpy’ 5
‘surus’ 4
5
4
Each value in the
A note on mutability/immutability dict is a int object
feeding_dict 1
‘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
‘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!
● Images of different coral reefs, some with coral bleaching and some without
● Images of different coral reefs, some with coral bleaching and some without
● Images of different coral reefs, some with coral bleaching and some without
● 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.
● 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)
d.get(key)