wk1.3

warm - up

  • Create a dictionary called numbers with the keys 'one', 'two', 'three', and associated values 1, 2, 3 three different ways.

In [7]:
a = {'one':1, 'two':2, 'three': 3}
b = dict(one=1, two=2, three= 3)
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
a == b == c


Out[7]:
True
  • Get the value from the dictionary for key 'one'.

In [8]:
a['one']


Out[8]:
1
  • Update the value for key 'two' to 'dos'

In [11]:
a['two'] = 'dos'
a


Out[11]:
{'one': 1, 'three': 3, 'two': 'dos'}
  • Add a new key value pair for 'four'/4

In [12]:
a['four'] = 4
a


Out[12]:
{'four': 4, 'one': 1, 'three': 3, 'two': 'dos'}
  • delete the key value pair 'one'/1

In [13]:
del a['one']
a


Out[13]:
{'four': 4, 'three': 3, 'two': 'dos'}
  • iterate over all the keys using a for loop

In [16]:
for k in a.keys():
    print(k)
    
for k in a:
    print(k)


three
four
two
three
four
two
  • iterate over all the values using a for loop

In [19]:
for k in a:
    print(a[k])
    
print()

for val in a.values():
    print(val)
    
print()

for k, val in a.items():
    print(val)


3
4
dos

3
4
dos

3
4
dos
  • iterate over all the key/value pairs (as a tuple) with a for loop (two different ways)

In [23]:
for k, val in a.items():
    print((k, val))
    
print()

for k in a:
    print((k, a[k]))

print()

for elem in a.items():
    print(elem)


('three', 3)
('four', 4)
('two', 'dos')

('three', 3)
('four', 4)
('two', 'dos')

('three', 3)
('four', 4)
('two', 'dos')
  • Dictionary keys can be mutable (True/False?).
  • Dictionary values can be mutable (True/False?).
  • Dictionaries are an example of a homogeneous datatype (True/False?).
  • Looking up a value in a list is generally faster than looking up a value in a dictionary (True/False?).
  • Suppose I set dict2 = numbers. If I change dict2 will numbers stay the same? Why or why not?

In [36]:
# Aliasing
a = {}
b = a
b['hi'] = 1
b

# Copy
a = {}
b = a.copy()
b['hi'] = 1
b

# Aliasing
a = []
b = a
b.append(1)
b

# copy
a = []
b = a[:]
b.append(1)
a

# copy
a = []
b = a.copy()
b.append(1)
a

# Immutable datastructures (ex. tuples) can't be updated so no way to alias.
a = ()
b = a
b = b + (1,)
a


Out[36]:
()

Morning activity: pair code review

Pair up with another group and review each others code.

Grading

35 points possible 15: Program runs and consistently produces correct output.

5 point penalty for any of the following:

  • Test cases running in addition to program functionality. (You need to 'comment out' any 'test' print statements for your final testing and turn-in.)
  • Incorrect or missing counts of emails or dates.
  • Other extra output, missing output, or output formatted differently than the examples above.
  • 10: Follows PEP8 coding guidelines, including author identification and header.
  • 10: Clarity. The program should not only be consistent with the requirements and approach described here, but it should be very easy to read the program and verify its consistency with the spec.

Afternoon

a word on namespaces

Some tips on file i/o

Next pair project: Jumbler

Github


In [ ]: