CSE 163
Tuples, Sets, and Dictionaries!
Oh My!�
Hunter Schafer
🎶 Listening to: Honne
💬 Before Class: How many holes does a straw have? Why?
This Time
Last Time
2
Recap
Learned about 4 data structures so far!
3
List Comprehensions
squares = [i ** 2 for i in range(1, 11) if i % 2 == 0]
squares = [
i ** 2
for i in range(1, 11)
if i % 2 == 0
]
4
1) What are you looping over?
2) [Optional] Skip if this is false
3) Expression with loop variable
4) Put it all in a list
Tuples
5
t = (1, "hello", 3.4)
print(t) # (1, 'hello', 3.4)
print(t[0]) # 1
t[1] = 'goodbye' # Error!
t = (1, "hello", 3.4)
print(t) # (1, 'hello', 3.4)
print(t[0]) # 1
t[1] = 'goodbye' # Error!
# You can "unpack" tuples to get their contents
a, b, c = t
print(b) # hello
Set
Set Methods
6
set() | Makes an empty set |
s.add(x) | Adds x to s |
s.remove(x) | Removes x from s |
s.clear() | Removes all values from s |
Dictionary
7
d = {}
d['a'] = 1
d['b'] = 2
print(d) # {'a': 1, 'b': 2}
Group Work:
Best Practices
When you first working with this group:
Tips:
8