-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhonebookExample.py
More file actions
23 lines (19 loc) · 852 Bytes
/
PhonebookExample.py
File metadata and controls
23 lines (19 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#From 12.6 Dictionaries and tuples
# Create a phonebook dictionary that has uses a last-name, first-name
# tupple for the index into the phonebook.
# Note: You can use tuples as keys in dictionaries (primarily because you can’t use lists)
print("Phone book example using a dictionary:")
last = 'Jones'; first = 'Sam'
name = (last, first)
number = "+1 111-111-1111"
t = [(name, number)]
phonedir = dict(t)
# Add names to the phone director dictionary
phonedir["Doe", "John"] = "+1 111-111-2222"
phonedir["Doe", "Jill"] = "+1 111-111-3333"
phonedir["Smith", "Frank"] = "+1 111-222-5555"
print("Jill Doe's phone number:", phonedir["Doe", "Jill"])
print("\nDictionary lookup for Phone Directory (Entries = ", len(phonedir), "):",sep='' )
print("Name \t\t Phone Number")
for last, first in phonedir:
print(first, last, '\t', phonedir[last,first])