Contents
- ascii_dictionary.py
- using_dictionary.py
- years_dictionary.py
ascii_dictionary.py 1/3
[top][prev][next]
# Demonstrate use of dictionary, using ASCII values
# By Sara Sprenkle
# create an empty dictionary
charToAscii = {}
ordValue = ord('a')
while ordValue <= ord('z'):
# add mapping to dictionary of chr(ordValue) --> ordValue (ordinal value)
char = chr(ordValue)
charToAscii[char] = ordValue
ordValue += 1
print(charToAscii)
print(sorted(charToAscii))
# demonstrate what happens if you try to look up a value that doesn't exist
print( charToAscii.get('!') )
print( charToAscii['!'] )
print("We don't get to here.")
using_dictionary.py 2/3
[top][prev][next]
# Demonstrate use of dictionary using book index
# by Sara Sprenkle
# create an empty dictionary
topicToPageNumber = {}
# add entries to the dictionary
topicToPageNumber["integer"] = 20
topicToPageNumber["list"] = 60
topicToPageNumber["string"] = 35
topicToPageNumber["dictionary"] = 58
topicToPageNumber["float"] = 20
print("~~~~ topicToPageNumber dictionary: ~~~~~~")
# iterates through the keys in the dictionary
for topic in topicToPageNumber:
# print the key and its associated value
print(topic, topicToPageNumber[topic])
print()
# display the type that is returned by dictionary methods
print("type of dictionary.keys():", type(topicToPageNumber.keys()))
print("type of dictionary.values():", type(topicToPageNumber.values()))
print("The number of keys is", len(topicToPageNumber.keys()))
# iterate through the values
print("\nIterate through the values:")
for val in topicToPageNumber.values():
print(val)
print("\nLook at the keys:")
keyList = list(topicToPageNumber.keys())
print("as <dict_keys>:\n", topicToPageNumber.keys())
print("as a list:\n", keyList)
print("\nPrint dictionary alphabetically by keys:")
# printing in order by key
keysSorted = list(topicToPageNumber.keys())
keysSorted.sort()
for topic in keysSorted:
# print the key and its associated value
print(topic, topicToPageNumber[topic])
print("\nPrint dictionary alphabetically by keys, again, in a different way:")
for topic in sorted(topicToPageNumber):
print(topic, topicToPageNumber[topic])
print("\nWhat happens if you try to access a key that doesn't exist?")
# demonstrate what happens if you try to look up a value that doesn't exist
print( "Using get:", topicToPageNumber.get('anothertopic') )
print( "Using indexing:", topicToPageNumber['anothertopic'] )
years_dictionary.py 3/3
[top][prev][next]
# Given a file of the form <firstname> <class>
# creates a mapping between the first names and class
# (In progress...)
# by CSCI 111
FILENAME="data/roster.dat"
# open the file for reading
yearsFile = open(FILENAME, "r")
nameToGradYear = {}
# for each line in the file
for line in yearsFile:
# split the line into the two elements
nameAndGradYearList = line.split()
name = nameAndGradYearList[0]
gradYear = nameAndGradYearList[1]
# add the student name as the key and the grad year as the value
nameToGradYear[name] = gradYear
print(nameToGradYear)
print("This program will repeatedly prompt you for a")
print("student's first name and tell you their graduation year.")
print("It stops if you enter nothing for a name.")
#repeatedly (until the user enters nothing)
while True:
# prompt for the student name and display their class year
studentName = input("What is the student's name? ")
if studentName == "":
break
Generated by GNU Enscript 1.6.6.