字典——基础

#! /usr/bin/env python
#coding=utf-8
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 '\n There 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:#或者in ab.keys()

    print '\n Guido\'s address is %s'% ab['Guido']

或:

    print '\n Guido\'s address is %(Guido)s ' % ab


你可能感兴趣的:(字典——基础)