Contents

  1. ascii_dictionary.py
  2. using_dictionary.py
  3. years_dictionary2.py
  4. years_dictionary.py

ascii_dictionary.py 1/4

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
# Sara Sprenkle

# create an empty dictionary
ascii= {}

ordValue = ord('a')

while ordValue <= ord('z'):
    # add mapping to dictionary of chr(ordValue) --> ordValue (ordinal value)
    char = chr(ordValue)
    ascii[char] = ordValue
    ordValue += 1

print(sorted(ascii))
print(ascii)

using_dictionary.py 2/4

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
#

# create an empty dictionary
ascii= {}

x = ord('a')

while x <= ord('z'):
    # add mapping to dictionary of chr(x) --> x (ordinal value)
    char = chr(x)
    ascii[char] = x
    x+=1

# iterates through the keys in the dictionary
for letter in ascii:
    # print the key and its associated value
    print(letter, ascii[letter])

# display the type that is returned by dictionary methods
print(type(ascii.keys()))
print(type(ascii.values()))
print("The number of keys is", len(ascii.keys()))

# iterate through the values
print("Iterate through the values:")
for val in ascii.values():
    print(val)
    
keyList = list(ascii.keys())
print("as <dict_keys>:\n", ascii.keys())
print("as a list:\n", keyList)

# printing in order by key
keysSorted = list(ascii.keys())
keysSorted.sort()

for letter in keysSorted:  # alternative: sorted(keysSorted)
    # print the key and its associated value
    print(letter, ascii[letter])

    
for letter in sorted(ascii):
    print(letter, ascii[letter])


years_dictionary2.py 3/4

[
top][prev][next]
# Given a file of the form <firstname> <year>
# creates a mapping between the first names and class
# by CSCI 111

FILENAME="data/roster.dat"

# open the file for reading
yearsFile = open(FILENAME, "r")

firstNameToClass = {}

for line in yearsFile:
    firstNameAndClass = line.split()
    firstName = firstNameAndClass[0]
    className = firstNameAndClass[1]
    firstNameToClass[firstName] = className

yearsFile.close()

for firstName in sorted(firstNameToClass):
    print(firstName, firstNameToClass[firstName])
    
whichName = input("For which student would you like to know their class year? ")

print( whichName, "is in the", firstNameToClass[whichName], "class")

years_dictionary.py 4/4

[
top][prev][next]
# Given a file of the form <firstname> <class>
# creates a mapping between the first names and class
# by CSCI 111

FILENAME="data/roster.dat"

# open the file for reading
yearsFile = open(FILENAME, "r")

firstNameToClass = {}

for line in yearsFile:
    firstNameAndClass = line.split()
    firstName = firstNameAndClass[0]
    className = firstNameAndClass[1]
    firstNameToClass[firstName] = className

yearsFile.close()

for firstName in sorted(firstNameToClass):
    print(firstName, firstNameToClass[firstName])
    
print()
print("Which students would you like to learn about?")
print("Press enter to exit.")
    
whichName = input("For which student would you like to know their class year? ")

while whichName != "":
    print( whichName, "is in the", firstNameToClass[whichName], "class")
    whichName = input("For which student would you like to know their class year? ")

print("Thank you!  Come again!")

Generated by GNU Enscript 1.6.6.