Python Programming - Dictionaries

685

6

Posted in TechnologySoftware

Introduction: Python Programming - Dictionaries

About: Occupation: Tech Support

# 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() )

Share

    Recommendations

    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Spotless Contest

      Spotless Contest
    • Space Challenge

      Space Challenge
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments