Friday, 6 September 2013

working with Dictionary

school={'teacher':7,'students':20,'Janitor':1,'cook':3,'principal':1, 'cashier':2}
print school

# up dating a dictionary
school['sponser']=6
print school

# prints key in the & value in the dict
print school.keys()
# print value in the dict
print school.values()

# print value of a key
print school['students']

# deleting an item from dict
del(school['Janitor'])
print school

# count the # of the dict
print len(school)

# five menu to the list
menu={}
menu['cornbread']=150
menu['coatsoup']=450
menu['vegetablesalade']=650
menu['dryrice']=100
menu['vento']=180
menu['orangefanta']=40
menu['malta']=190
menu['fufusoup']=250
menu['extrajuice']=290
print menu

for key,value in menu.items():
    print key,value

# adding list to a dictionary
citizens={'Liberia':'Liberians','Nigeria':'Nigerins','Americans':['chinese','europeans','Africans','Asians']}
print citizens

# using for loop
for key,value in citizens.items():
    print key,value

citizens['Americans'].sort()
print citizens

# update citizens
citizens['brazil']=['brazilians','southAmericans']
print citizens

No comments:

Post a Comment