Python Programming - Dictionaries

by matt392 in Circuits > Software

1128 Views, 6 Favorites, 0 Comments

Python Programming - Dictionaries

dictionary1-660x330.png

# dictionary exercise
# from "Learning Python Quickly"
# created using IDLE, the Python IDE

print("Creating an empty dictionary.")
telnums = {}

print ("Entering names and members into dictionary.")
telnums = {"Tom Jones" : "212-867-5309",
"Bob Smith" : "646-786-3059",
"Susan Stone" : "212-979-8200",
"Tammy Tang" : "718-228-6300",
"Jimmy Slick" : "212-555-2323"}

print ("The contents of the dictionary telnums is: ", telnums.items() )

print ("Displaying the dictionary vertically.")

for alpha in telnums:
print(alpha, telnums[alpha])

print ("\nNow we are going to correct an antry.")

telnums["Susan Stone"] = "646-797-2800"

print ("Now the dictionary is: ")
for alpha in telnums:
print (alpha, telnums[alpha])

print ("\nNow we are going to retrieve Bob Smith's phone number: ")
print (telnums["Bob Smith"])

print ("\nDeleting Bob Smith now: ")
del telnums["Bob Smith"]
print ("Now the dictionary is: ")
for alpha in telnums:
print (alpha, telnums[alpha])

print ("\nListing the key names: ")
print (telnums.keys() )
for keys in telnums:
print(keys)

print ("\nListing the values: ")
print (telnums.values() )
for values in telnums:
print(telnums[values])

print ("\nEmpty the dictionary.")
telnums.clear()
print ("\nChecking to make sure the dictionary is clear: ")
print (telnums.items() )