天天看点

《Python编程:从入门到实践》课后习题-第6章6-1 人6-2 喜欢的数字6-3 词汇表6-4 词汇表26-5 河流6-6 调查6-7 人6-8 宠物6-9 喜欢的地方6-10 喜欢的数字6-11 城市

6-1 人

#6-1 人

people = {'first_name':'Whitney','last_name':'Houston','age':,'city':'New Jersey'}
print people
           

6-2 喜欢的数字

#6-2 喜欢的数字

dic = {'Rebecca':,'Rihana':,'Arriana':,'Cristina':,'Jessica':}
print dic
           

6-3 词汇表

#6-3 词汇表

dic = {'lower':'全部小写','sort':'永久性排序','strip':'删除首尾空白','title':'首字母大写','pop':'删除'}
print ('lower : ' + dic['lower'])
print ('\nsort : ' + dic['sort'])
print ('\nstrip : ' + dic['strip'])
print ('\ntitle : ' + dic['title'])
print ('\npop : ' + dic['pop'])
           

6-4 词汇表2

#6-4 词汇表2

dic = {'lower':'全部小写','sort':'永久性排序','strip':'删除首尾空白','title':'首字母大写','pop':'删除',}
for key,value in dic.items():
    print(key + ' : ' + value)
print ("\n")
dic['min']='最小值'
dic['max']='最大值'
dic['sum']='总和'
dic['insert']='插入元素'
dic['sorted']='临时排序'
for key,value in dic.items():
    print( key + ' : ' + value)
           

6-5 河流

#6-5 河流

dic = {'The Mississippi River':'America','The Nile':'Egypt','The Yangtze River':'China'}
for river,country in dic.items():
    print(river + " runs through " + country + ".")
for river in dic.keys():
    print (river)
for country in dic.values():
    print (country)
           

6-6 调查

#6-6 调查

favorite_languages = {'jen':'python','sarah':'c','edward':'ruby','phil':'python',}
list = ['jen','rebecca','edward','cristina']
for name,language in favorite_languages.items():
    if name in list:
        print(name + ", Thank you very much!")
    else:
        print(name + ", Welcome to join us!")
           

6-7 人

#6-7 人

people1 = {'first_name':'Whitney','last_name':'Houston','age':,'city':'New Jersey'}
people2 = {'first_name':'Jane','last_name':'Zhang','age':,'city':'Chengdu'}
people3 = {'first_name':'Stefani Joanne Angelina','last_name':'Germanotta','age':,'city':'Manhattan'}
people = [people1,people2,people3]
for information in people:
    print(information)
           

6-8 宠物

#6-8 宠物

milk = {'type':'cat','master\'s name':'Rebecca'}
jerry = {'type':'dog','master\'s name':'Cristina'}
dudu = {'type':'bird','master\'s name':'Jenny'}
momo = {'type':'tortoise','master\'s name':'Arriana'}
lists = [milk,jerry,dudu,momo]
for animal in lists:
    print (animal)
           

6-9 喜欢的地方

#6-9 喜欢的地方

favorite_places = {'Rebecca':['Japan','China','Egypt'],'Cristina':['France','America','China'],'Arriana':['England','China']}
for name,countries in favorite_places.items():
    print (name)
    for country in countries:
        print (country)
           

6-10 喜欢的数字

#6-10 喜欢的数字

dic = {'Rebecca':[,,],'Rihana':[,,],'Arriana':[,],'Cristina':[,],'Jessica':[,,]}
for name,favorite_numbers in dic.items():
    print (name)
    for number in favorite_numbers:
        print(number)
           

6-11 城市

#6-11 城市

cities = {
    'Wuhan':{
        'country':'China',
        'population':'14 hundred millions',
        'fact':'A beautiful country.',
        },
    'New York':{
        'country':'America',
        'population':'3 hundred millions',
        'fact':'Has 50 states.'
        },
    'Tokyo':{
        'country':'Japan',
        'population':'1 hundred millions',
        'fact':'Has some attractions.',
        },
    }

for city,values in cities.items():
    print (city)
    for key,value in values.items():
        print (key)
        print (value)