Your SlideShare is downloading. ×
Python Dictionary
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Python Dictionary

953
views

Published on

National Cheng-Chi University …

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

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
953
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
29
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. DictionaryPython Built-in Data Type
    • 2. What does Dict do?• 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 each item with for-loop statement >>> for x in aDict.keys(): ... print(x) ... apple banana Dictionary Introduction
    • 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. 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 a item 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. 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. 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. Printif menu_choice == 1: print("Telephone Numbers:") for x in numbers.keys(): print("Name: ", x, "tNumber:", numbers[x]) print() Dictionary Introduction
    • 15. Addelif menu_choice == 2: print("Add Name and Number") name = input("Name: ") phone = input("Number: ") numbers[name] = phone Dictionary Introduction
    • 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. 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

    ×