ds_using_dict.py 591 B

123456789101112131415161718192021222324
  1. # 'ab' is short for 'a'ddress'b'ook
  2. ab = { 'Swaroop' : 'swaroop@swaroopch.com',
  3. 'Larry' : 'larry@wall.org',
  4. 'Matsumoto' : 'matz@ruby-lang.org',
  5. 'Spammer' : 'spammer@hotmail.com'
  6. }
  7. print "Swaroop's address is", ab['Swaroop']
  8. # Deleting a key-value pair
  9. del ab['Spammer']
  10. print '\nThere are {} contacts in the address-book\n'.format(len(ab))
  11. for name, address in ab.items():
  12. print 'Contact {} at {}'.format(name, address)
  13. # Adding a key-value pair
  14. ab['Guido'] = 'guido@python.org'
  15. if 'Guido' in ab:
  16. print "\nGuido's address is", ab['Guido']