python编程 从入门到实践 第六章 课后习题(6-1~6-12)

动手练一练

# 动手试一试
#6—1
lingling={
    'first_name':'lingling',
    'last_name':'huang',
    'age':'13',
    'city':'dezhou',
}
print(lingling)

#6—2 喜欢的数字
friends={
    'lingling':'5',
    'daming': '3',
    'sam': '2',
    'tom': '1',
}
print(friends)

#6_3 词汇表
words={
    'dog':'wangwang',
    'cat':'miaomiao',
    'bird':'jiji',
    'dragon':'wawa',
}
for word in words:
    print(word+" : "+words[word])

for word in words:
    print(word+"\n\t"+words[word])

#  动手试一试
# 6-4 词汇表 2:
favorite_languages={
        'jen':'python',
        'sarah':'c',
        'daming':'java',
        'sam':'c++',
}
for name,language in favorite_languages.items():
    print(name+' : '+language+' .')


# 6-5 河流
rivers={
    'huanghe':'china',
    'nile':'egyp',
    'orange':'apple',
}
for river,country in rivers.items():
    print(" the "+river+"runs through "+country+' .')
for river in rivers.keys():
    print(river)
for country in rivers.values():
    print(country)

# 6-6 调查:
favorite_languages={
        'jen':'python',
        'sarah':'c',
        'daming':'java',
        'sam':'c++',
}
people={'tom','sam','daming','lingling'}
for man in people:
    if man in favorite_languages.keys():
        print("thanks "+man)
    else:
        print("would you like to join us? "+man)

##  动手试一试
# 6-7 人:
lingling={
         'first_name':'lingling',
        'last_name':'huang',
        'age':'13',
        'city':'dezhou',
}
daming={
        'first_name': 'daming',
        'last_name': 'li',
        'age': '12',
        'city': 'beihai',
}
sam={
        'first_name': 'sam',
        'last_name': 'wang',
        'age': '13',
        'city': 'tianjin',
}
people=[sam,daming,lingling]
for p in people:
    print(p)

# 6-8 宠物
peter={'kind':'dog','zhuren':'lingling'}
qiaoen={'kind':'pig','zhuren':'daming'}
tuomasi={'kind':'cat','zhuren':'tom'}
pets=[peter,qiaoen,tuomasi]
for p in pets:
    print(p)

# 6-9 喜欢的地方
favorite_places={
    'daming':['beijing','beihai'],
    'lingling':['shanghai','chengdu','chongqing'],
    'tom':['dezhou','tianjin'],
}
for place in favorite_places.items():
    print(place)

# 6-10 喜欢的数字
friends={
    'lingling':['5','3'],
    'daming': ['3','4','2'],
    'sam': ['2','4','5','6'],
    'tom': ['1'],
}
for friend in friends.items():
    print(friend)

#6-11 城市
cities={
    'dezhou':{
        'country':'china',
        'population':'13 billion',
        'fact':'great',
    },
    'newyork':{
        'country':'america',
        'population':'3 billion',
        'fact':'wide',
    },
    'dongjing':{
        'country':'japan',
        'population':'1 billion',
        'fact':'little',
    },
}
for city_name,city_info in cities.items():
    print('\n\tcity name: '+city_name)
    print("\tcountry: "+city_info['country'])
    print("\tpopulation: "+city_info['population'])
    print("\tfact: " + city_info['fact'])

 

GOOD LUCK!!!

你可能感兴趣的:(Python)