CSE 163
Section XX
TA1 & TA2
Question of the Day: If you were a data structure, which one would you be and why?
Announcements
Recap
What we’ve Learned so far:
Game Plan
What We’ll Cover Today
After section, please fill out the groupmate evaluations for today through the Canvas assignment!
�
List
Use Cases:
new_list = list()
new_list = []
new_list.append("a") # ["a"]
new_list.append(2) # ["a", 2]
new_list[0] # "a"
new_list.pop(0) # returns “a”,
new_list is [2]
Tuple
Use Cases:
tup = ("hello", "world")
tup[0] # "hello"
# takes in params a and b
def pack(a, b):
# returns a tuple of (a, b)
return a, b
Sets
difference()
Use Cases:
my_set = set()
my_set.add(10) # {10}
my_set.add(30) # {10, 30}
my_set.add(30) # {10, 30}
my_set.add([]) # Error!
if 10 in my_set: # Membership Query
print("Found 10!")
Dictionaries
Use Cases:
d = dict()
d = {}
d['Triangle'] = 3
d['Square'] = 4
d['Square'] # 4
items = ["cat", "hat", "bat", "cat"]
my_dict = dict()
for i in items: # also really fast!
if i not in my_dict:
my_dict[i] = 0
my_dict[i] += 1
my_dict
# {"cat": 2, "hat": 1, "bat": 1}
File Processing
file_path = "home/doggies.txt"
with open(file_path) as f:
lines = f.readlines()
for l in lines:
print(l)
file_path = "home/doggies.txt"
with open(file_path) as f:
words = f.read().split()
for word in words:
print(word)