KEMBAR78
Python Dictionary | KEY
Dictionary
Python Built-in Data Type
What does Dict do?

• Collection of information
• Query information
• Add/Update Information

                              Dictionary
                              Introduction
Initialization

•   aDict = {}

•   aDict = dict()

•   aDict = { “apple” : 3
              “banana”: 5 }


                              Dictionary
                              Introduction
Initialization

•   aDict = {}

•   aDict = dict()

•   aDict = { “apple” : 3
              “banana”: 5 }
                 Key
                              Dictionary
                              Introduction
Initialization

•   aDict = {}

•   aDict = dict()

•   aDict = { “apple” : 3
              “banana”: 5 }
                 Key   Value
                               Dictionary
                               Introduction
Query
>>> aDict["apple"]
3
>>> aDict.keys()
dict_keys(['apple', 'banana'])
>>> aDict.values()
dict_values([3, 5])
>>> aDict.items()
dict_items([('apple', 3), ('banana', 5)])

                                    Dictionary
                                    Introduction
Query
• Call each item with for-loop statement
   >>> for x in aDict.keys():
   ...    print(x)
   ...
   apple
   banana



                                       Dictionary
                                       Introduction
Add
• Directly add a item to dictionary
  >>> aDict
  {'apple': 5, 'banana': 5}
  >>> aDict['waterfruit'] = 8
  >>> aDict
  {'waterfruit': 8, 'apple': 5, 'banana': 5}




                                        Dictionary
                                        Introduction
Update
•   Update the content of the dictionary
•   aDict.update(<dict>)

•   Example:
     >>> aDict.update({'apple':5})
     >>> aDict.update({'chocolate':10})
     >>> aDict
     {'chocolate': 10, 'apple': 5, 'banana': 5}



                                        Dictionary
                                        Introduction
Delete

• Remove a item from the dictionary
•  del aDict[“apple”]




                                      Dictionary
                                      Introduction
Practice
• Phone book
• Functions:
 • Print a phone number
 • Add a phone number
 • Remove a phone number
 • Lookup a phone number
                           Dictionary
                           Introduction
Practice
def print_menu():
    print('1. Print Phone Numbers')
    print('2. Add a Phone Number')
    print('3. Remove a Phone Number')
    print('4. Lookup a Phone Number')
    print('5. Quit')
    print()



                                  Dictionary
                                  Introduction
Practice
numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 5:
    menu_choice = int(input("Type in a number (1-5): "))
    if menu_choice == 1:
        pass
    elif menu_choice == 2:
        pass
    elif menu_choice == 3:      http://goo.gl/A9xDr
        pass
    elif menu_choice == 4:
        pass
    elif menu_choice != 5:
        print_menu()
                                                Dictionary
                                                Introduction
Print
if menu_choice == 1:
     print("Telephone Numbers:")
     for x in numbers.keys():
         print("Name: ", x, "tNumber:", numbers[x])
     print()




                                             Dictionary
                                             Introduction
Add

elif menu_choice == 2:
     print("Add Name and Number")
     name = input("Name: ")
     phone = input("Number: ")
     numbers[name] = phone




                                    Dictionary
                                    Introduction
Remove
elif menu_choice == 3:
        print("Remove Name and Number")
        name = input("Name: ")
        if name in numbers:
            del numbers[name]
        else:
            print(name, "was not found")




                                           Dictionary
                                           Introduction
Lookup
elif menu_choice == 4:
    print("Lookup Number")
    name = input("Name: ")
    if name in numbers:
        print("The number is", numbers[name])
    else:
        print(name, "was not found")




                                          Dictionary
                                          Introduction

Python Dictionary

  • 1.
  • 2.
    What does Dictdo? • Collection of information • Query information • Add/Update Information Dictionary Introduction
  • 3.
    Initialization • aDict = {} • aDict = dict() • aDict = { “apple” : 3 “banana”: 5 } Dictionary Introduction
  • 4.
    Initialization • aDict = {} • aDict = dict() • aDict = { “apple” : 3 “banana”: 5 } Key Dictionary Introduction
  • 5.
    Initialization • aDict = {} • aDict = dict() • aDict = { “apple” : 3 “banana”: 5 } Key Value Dictionary Introduction
  • 6.
    Query >>> aDict["apple"] 3 >>> aDict.keys() dict_keys(['apple','banana']) >>> aDict.values() dict_values([3, 5]) >>> aDict.items() dict_items([('apple', 3), ('banana', 5)]) Dictionary Introduction
  • 7.
    Query • Call eachitem with for-loop statement >>> for x in aDict.keys(): ... print(x) ... apple banana Dictionary Introduction
  • 8.
    Add • Directly adda item to dictionary >>> aDict {'apple': 5, 'banana': 5} >>> aDict['waterfruit'] = 8 >>> aDict {'waterfruit': 8, 'apple': 5, 'banana': 5} Dictionary Introduction
  • 9.
    Update • Update the content of the dictionary • aDict.update(<dict>) • Example: >>> aDict.update({'apple':5}) >>> aDict.update({'chocolate':10}) >>> aDict {'chocolate': 10, 'apple': 5, 'banana': 5} Dictionary Introduction
  • 10.
    Delete • Remove aitem from the dictionary • del aDict[“apple”] Dictionary Introduction
  • 11.
    Practice • Phone book •Functions: • Print a phone number • Add a phone number • Remove a phone number • Lookup a phone number Dictionary Introduction
  • 12.
    Practice def print_menu(): print('1. Print Phone Numbers') print('2. Add a Phone Number') print('3. Remove a Phone Number') print('4. Lookup a Phone Number') print('5. Quit') print() Dictionary Introduction
  • 13.
    Practice numbers = {} menu_choice= 0 print_menu() while menu_choice != 5: menu_choice = int(input("Type in a number (1-5): ")) if menu_choice == 1: pass elif menu_choice == 2: pass elif menu_choice == 3: http://goo.gl/A9xDr pass elif menu_choice == 4: pass elif menu_choice != 5: print_menu() Dictionary Introduction
  • 14.
    Print if menu_choice ==1: print("Telephone Numbers:") for x in numbers.keys(): print("Name: ", x, "tNumber:", numbers[x]) print() Dictionary Introduction
  • 15.
    Add elif menu_choice ==2: print("Add Name and Number") name = input("Name: ") phone = input("Number: ") numbers[name] = phone Dictionary Introduction
  • 16.
    Remove elif menu_choice ==3: print("Remove Name and Number") name = input("Name: ") if name in numbers: del numbers[name] else: print(name, "was not found") Dictionary Introduction
  • 17.
    Lookup elif menu_choice ==4: print("Lookup Number") name = input("Name: ") if name in numbers: print("The number is", numbers[name]) else: print(name, "was not found") Dictionary Introduction