<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""A demonstration of some basics of dicts"""

# The following dict maps (fictional) student numbers to names
student_number_to_name = {993845932: 'Dave',
                          1029475983: 'Idil',
                          1028385739: 'Alyson',
                          994738583: 'Sarmila',
                          993845878: 'Ann',
                          1001304928: 'Payal',
                          }

# Determine whether a key is in the dict using the "in" operator:
print(993845932 in student_number_to_name)
print(0 in student_number_to_name)
# Note that this does NOT check values; only keys!
print('Dave' in student_number_to_name)

# Look up a value by "indexing" the dict using the key in square brackets:
print(student_number_to_name[993845932])

# What happens if we try to look up a value for a key that isn't in the dict?
# print(student_number_to_name[0])  # KeyError
# Check whether a key is in the dictionary before trying to access it in order
# to avoid this error

# Add an item to the dict:
student_number_to_name[9999] = 'FakeStudent'
print('dict after adding a value:', student_number_to_name)

# What happens if we try to add a mutable key to the dict?
# student_number_to_name[[9999]] = 'FakeStudentList'
# Can't do it. Keys must be immutable


# Reassign a key to a different value:
student_number_to_name[9999] = 'FakeStudent2'
print('dict after reassigning FakeStudent:', student_number_to_name)

# Determine how many key-value items in dict by using built-in function len
print(len(student_number_to_name))  # This should print 7

# Iterate through the keys of a dict:
for key in student_number_to_name:
    print('key:', key)

# Note that, in versions of Python 3.6 and before, the order of iteration
# wasn't guaranteed to be the same as the order in which you entered the
# entries. In 3.7 and later, it is guaranteed that insertion order is 
# preserved.
# For the purposes of this class, you shouldn't assume that the insertion order
# will be preserved; assume that the keys might be iterated over in any order

# Iterate through the key-value items of a dict using the dict method items():


# Iterate through the values of a dict using the dict method values():

</pre></body></html>