Overview
Teaching: 15 min
Exercises: 20 minQuestions
How can I store and manipulate non-rectangular data?
Objectives
Write programs that use sets to store unique values.
Write programs that use dictionaries to store key/value pairs.
Identify values that can and cannot be used as dictionary keys.
{...}
set()
to create an empty set.primes = {2, 3, 5, 7}
print('is 3 prime?', 3 in primes)
print('is 9 prime?', 9 in primes)
is 3 prime? True
is 9 prime? False
odds = {3, 5, 7, 9}
print('intersection', odds & primes)
print('union', odds | primes)
intersection {3, 5, 7}
union {3, 5, 7, 9, 11}
primes.add(11)
print('primes becomes', primes)
primes.discard(7)
print('after removal', primes)
primes.add(11)
print('after adding 11 again', primes)
primes becomes {11, 2, 3, 5, 7}
after removal {11, 2, 3, 5}
after adding 11 again {11, 2, 3, 5}
names = {'Darwin', 'Newton', 'Turing'}
for n in names:
print(n)
Newton
Darwin
Turing
birthdays = {'Newton' : 1602, 'Darwin' : 1809}
print(birthdays['Newton'])
birthdays['Turing'] = 1612 # oops
birthdays['Turing'] = 1912 # that's better
print(birthdays)
1602
{'Darwin': 1809, 'Newton': 1602, 'Turing': 1912}
people = {('Isaac', 'Newton'): 1602, ('Charles', 'Darwin'): 1809}
numbers = [1, 0, 1, 2, 0, 0, 1, 2, 1, 3, 1, 0, 2]
count = {}
for n in numbers:
if n not in count:
count[n] = 1
else:
count[n] = count[n] + 1
print(count)
{0: 4, 1: 5, 2: 3, 3: 1}
elements = {'H' : 1, 'He' : 2, 'Li' : 3, 'Be' : 4, 'B' : 5}
print('atomic number of lithium:', elements['Li'])
atomic number of lithium: 3
How Heavy Is This Molecule?
Write a function that takes two parameters: a dictionary mapping atomic symbols to atomic weights, and a list of (atom, count) pairs for a molecule, and returns that molecule’s molecular weight.
weights = {'H' : 1.0079, 'C' : 12.0107, 'N' : 14.0067, 'O' : 15.9994, 'P' : 30.9738, 'S' : 32.065} methane = [('C', 1), ('H', 4)] print('methane', mol_weight(weights, methane)) aminothiazole = [('C', 3), ('H', 4), ('N', 2), ('S', 1)] print('aminothiazole', mol_weight(weights, aminothiazole))
methane 16.0423 aminothiazole 100.1421
How did you test your function?
Key Points
Use sets to store unique unordered values.
Use dictionaries to store extra information with those values.