Python Notes - Dictionaries

Contents   Installing Python   Variables   Input/Output   if   while   Functions - def   Scopes (local/global)   Function parameters   Lists (arrays)   Dictionaries   Tuples   Files   Libraries   Hints, tips  

A dictionary holds pairs of items (typically, strings). We enclose them in {}
We can set up sume values, as in:

d = {"John":"UK", "Mai": "Thailand", "Pierre":"France" }
print d["John"]       # prints  UK      
Here, the person's name is the 'key', used as a basis for searching. Often the key needs to be unique.

Here is a program which creates a dictionary of English/French word pairs, then lets the user enter English words and see the French equavalent. Note:

  • the inputWords function takes a dictionary as parameter. It fills it with word pairs, ending when an Enter is pressed. It uses the has_key function to test if the English word has already been entered.
  • The rest of the main function lets the user look up a word. It uses has_key to see if the word exists. If it does, then we look up the word.
#dictionary - english to french
translate = {}   #empty dictionary

#------------------------------------------
def main():
    inputWords(translate)

    reply = raw_input("want to translate a word (y/n)? ")
    while reply =="y":
        english = raw_input("Type English word: ")
        if translate.has_key(english):
            print "French for", english ,"is", translate[english]
        else:    #missing
            print "Sorry - don't know ", english

  
        reply = raw_input("want to translate a word (y/n)? ")
    #end while

#--------------------------------------------
def inputWords(dic):
    print"Build your dictionary:"
    print"----------------------"
    english = raw_input("Type English word, or just enter to end: ")
    while english !="":
        if dic.has_key(english):
            print "Got ", english, " already"
        else:    #add it
            french = raw_input("Type French for "+ english+": ")
            dic[english] = french
            
        english = raw_input("Type English word, or just enter to end: ")
    #end while
    print"---------- Dictionary built ------------"
    print
#---------------------------------------------
        
main()
and a sample run is:

Build your dictionary:
----------------------
Type English word, or just enter to end: sea
Type French for sea: mer
Type English word, or just enter to end: wine
Type French for wine: vin
Type English word, or just enter to end: apple
Type French for apple: pomme
Type English word, or just enter to end: 
---------- Dictionary built ------------

want to translate a word (y/n)? y
Type English word: apple
French for apple is pomme
want to translate a word (y/n)? y
Type English word: fish
Sorry - don't know  fish
want to translate a word (y/n)? y
Type English word: wine
French for wine is vin
want to translate a word (y/n)? n

Contents   Installing Python   Variables   Input/Output   if   while   Functions - def   Scopes (local/global)   Function parameters   Lists (arrays)   Dictionaries   Tuples   Files   Libraries   Hints, tips