-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing_dict.py
32 lines (26 loc) · 1009 Bytes
/
using_dict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python
# Filename: using_dict.py
#'ab' is short for 'a'ddress'b'ook
ab = { 'Swaroop' : '[email protected]',
'Larry' : '[email protected]',
'Matsumoto' : '[email protected]',
'Spammer' : '[email protected]'
}
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = '[email protected]'
# Deleting a key/value pair
del ab['Spammer']
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
print 'Contact %s at %s' % (name, address)
if address == '[email protected]':
del ab[name]
print 'Delete %s sucsess' % name
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
print "\nGuido's address is %s" % ab['Guido']
if ab.has_key('Guido'): # OR ab.has_key('Guido')
print "\nGuido's address is %s" % ab['Guido']