pythonプログラミング入門から実践第6章授業後練習問題(6-1~6-12)

4075 ワード

手を出して練習する
#      
#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+"
\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('
\tcity name: '+city_name) print("\tcountry: "+city_info['country']) print("\tpopulation: "+city_info['population']) print("\tfact: " + city_info['fact'])

 
GOOD LUCK!!!