Code # ! /Usr/bin/Python
# Filename: dict. py
# 'AB' is communication book
AB = {
'Bill':'Billgates@microsoft.com'
,
' Kaifu-Lee ' : ' Kaifu-lee@google.com ' ,
' Minghui ' : ' Minghui@lypower.com '
}
Print " \ Nthere are % d contacts in the address-book: " % Len (AB)
For Name, email In AB. Items (): # Index all the elements
Print ' \ T % s ' % (Name, email)
Print " Bill's email address is % s " % AB ['Bill'] # Index by key
Print " \ Nadd Lei-Che's address: AB ['lei-Ch'] = 'chelei @ zhenghai.com' "
AB [ "Lei-Che"]= 'Chelei@zhenghai.com' # Append a dictionary
Print " There are % d contacts in the address-book: " % Len (AB)
For Name, email In AB. Items (): # Index all the elements
Print ' \ T % s ' % (Name, email)
Print " \ Nkill Kaifu-Lee's information: del AB ['kaifu-Lee '] "
DelAB ['Kaifu-Lee'] # Del a dict
Print " There are % d contacts in the address-book: " % Len (AB)
For Name, email In AB. Items (): # Index all the elements
Print ' \ T % s ' % (Name, email)
# -- = ----------- The output is below ------- = --
# There are 3 contacts in the address-book:
# Kaifu-Lee kaifu-lee@google.com
# Bill billgates@microsoft.com
# Minghui minghui@lypower.com
# Bill's email address is billgates@microsoft.com
# Add Lei-Che's address: AB ['lei-Ch'] = 'chelei @ zhenghai.com'
# There are 4 contacts in the address-book:
# Lei-che chelei@zhenghai.com
# Kaifu-Lee kaifu-lee@google.com
# Bill billgates@microsoft.com
# Minghui minghui@lypower.com
# Kill Kaifu-Lee's information: del AB ['kaifu-Lee ']
# There are 3 contacts in the address-book:
# Lei-che chelei@zhenghai.com
# Bill billgates@microsoft.com
# Minghui minghui@lypower.com