DictionaryPython Built-in Data Type
What does Dict do?• Collection of information• Query information• Add/Update Information                              Dict...
Initialization•   aDict = {}•   aDict = dict()•   aDict = { “apple” : 3              “banana”: 5 }                        ...
Initialization•   aDict = {}•   aDict = dict()•   aDict = { “apple” : 3              “banana”: 5 }                 Key    ...
Initialization•   aDict = {}•   aDict = dict()•   aDict = { “apple” : 3              “banana”: 5 }                 Key   V...
Query>>> aDict["apple"]3>>> aDict.keys()dict_keys([apple, banana])>>> aDict.values()dict_values([3, 5])>>> aDict.items()di...
Query• Call each item with for-loop statement   >>> for x in aDict.keys():   ...    print(x)   ...   apple   banana       ...
Add• Directly add a item to dictionary  >>> aDict  {apple: 5, banana: 5}  >>> aDict[waterfruit] = 8  >>> aDict  {waterfrui...
Update•   Update the content of the dictionary•   aDict.update(<dict>)•   Example:     >>> aDict.update({apple:5})     >>>...
Delete• Remove a item from the dictionary•  del aDict[“apple”]                                      Dictionary            ...
Practice• Phone book• Functions: • Print a phone number • Add a phone number • Remove a phone number • Lookup a phone numb...
Practicedef print_menu():    print(1. Print Phone Numbers)    print(2. Add a Phone Number)    print(3. Remove a Phone Numb...
Practicenumbers = {}menu_choice = 0print_menu()while menu_choice != 5:    menu_choice = int(input("Type in a number (1-5):...
Printif menu_choice == 1:     print("Telephone Numbers:")     for x in numbers.keys():         print("Name: ", x, "tNumber...
Addelif menu_choice == 2:     print("Add Name and Number")     name = input("Name: ")     phone = input("Number: ")     nu...
Removeelif menu_choice == 3:        print("Remove Name and Number")        name = input("Name: ")        if name in number...
Lookupelif menu_choice == 4:    print("Lookup Number")    name = input("Name: ")    if name in numbers:        print("The ...
Upcoming SlideShare
Loading in …5
×

Python Dictionary

1,300 views

Published on

National Cheng-Chi University
Computer Science Department
Python Programming for no-engineer
Assistant Course

Published in: Technology
  • Be the first to comment

Python Dictionary

  1. 1. DictionaryPython Built-in Data Type
  2. 2. What does Dict do?• Collection of information• Query information• Add/Update Information Dictionary Introduction
  3. 3. Initialization• aDict = {}• aDict = dict()• aDict = { “apple” : 3 “banana”: 5 } Dictionary Introduction
  4. 4. Initialization• aDict = {}• aDict = dict()• aDict = { “apple” : 3 “banana”: 5 } Key Dictionary Introduction
  5. 5. Initialization• aDict = {}• aDict = dict()• aDict = { “apple” : 3 “banana”: 5 } Key Value Dictionary Introduction
  6. 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. 7. Query• Call each item with for-loop statement >>> for x in aDict.keys(): ... print(x) ... apple banana Dictionary Introduction
  8. 8. Add• Directly add a item to dictionary >>> aDict {apple: 5, banana: 5} >>> aDict[waterfruit] = 8 >>> aDict {waterfruit: 8, apple: 5, banana: 5} Dictionary Introduction
  9. 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. 10. Delete• Remove a item from the dictionary• del aDict[“apple”] Dictionary Introduction
  11. 11. Practice• Phone book• Functions: • Print a phone number • Add a phone number • Remove a phone number • Lookup a phone number Dictionary Introduction
  12. 12. Practicedef 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. 13. Practicenumbers = {}menu_choice = 0print_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. 14. Printif menu_choice == 1: print("Telephone Numbers:") for x in numbers.keys(): print("Name: ", x, "tNumber:", numbers[x]) print() Dictionary Introduction
  15. 15. Addelif menu_choice == 2: print("Add Name and Number") name = input("Name: ") phone = input("Number: ") numbers[name] = phone Dictionary Introduction
  16. 16. Removeelif 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. 17. Lookupelif 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

×