6-1 人
per_info={'first_name':'chen','last_name':'nan',
'age':'18','city':'guang zhou'
}
for first,last in per_info.items():
print(first,':',last.title())
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
first_name : Chen
last_name : Nan
age : 18
city : Guang Zhou
Process finished with exit code 0
6-2 喜欢的数字
fri_num={
'chen':2,
'pei':8,
'sun':10,
'li':20,
'zhao':11,
}
for first,last in fri_num.items():
print(first.title(),':',last)
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
Chen : 2
Pei : 8
Sun : 10
Li : 20
Zhao : 11
Process finished with exit code 0
6-3 词汇表:
vol_table={
'if':'judge the statement!',
'for':'perform the loop',
'while':'perform the loop',
'int':'declare an integer variable',
'float':'declare a float variable'
}
for first,last in vol_table.items():
print(first,":")
print('\t',last)
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
if :
judge the statement!
for :
perform the loop
while :
perform the loop
int :
declare an integer variable
float :
declare a float variable
Process finished with exit code 0
6-5 河流:
vol_table={
'the Yangtze River':['china'],
'the Yellow River':['china'],
'Mekong River':['China','Laos','Burma','Thailand','Cambodia','Vietnam']
}
for first,last in vol_table.items():
print(first.title(),'run through ',end='')
for x in last:
print(x,end=', ')
print()
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
The Yangtze River run through china,
The Yellow River run through china,
Mekong River run through China, Laos, Burma, Thailand, Cambodia, Vietnam,
Process finished with exit code 0
6-6 调查:
favorite_languages={
'jen':'python',
'saran':'C',
'edward':'ruby',
'phil':'python',
}
search_list=['edward','phil','chen','li']
for x in search_list:
if x in favorite_languages.keys():
print(x,",thank you for your inquiry!")
else:
print(x,'please accept my investigation!')
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
edward ,thank you for your inquiry!
phil ,thank you for your inquiry!
chen please accept my investigation!
li please accept my investigation!
Process finished with exit code 0
6-7 人
per_info={'first_name':'chen','last_name':'nan',
'age':'18','city':'guang zhou'
}
per_info1={'first_name':'li','last_name':'ning',
'age':'20','city':'hang zhou'
}
per_info2={'first_name':'wang','last_name':'ning',
'age':'16','city':'wen zhou'
}
people=[per_info,per_info1,per_info2];
for person in people:
for first,last in person.items():
print(first,':',last.title())
print()
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
first_name : Chen
last_name : Nan
age : 18
city : Guang Zhou
first_name : Li
last_name : Ning
age : 20
city : Hang Zhou
first_name : Wang
last_name : Ning
age : 16
city : Wen Zhou
Process finished with exit code 0
6-9 喜欢的地方:
friends_favor={
'chan':['hang zhou','su zhou','wen zhou'],
'li':['guang zhou','shan tou'],
'wang':['bei jing','shang hai']
}
for x,y in friends_favor.items():
print(x.title(),"'s favorite places are:")
for place in y:
print(place,end=', ')
print()
print()
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
Chan 's favorite places are:
hang zhou, su zhou, wen zhou,
Li 's favorite places are:
guang zhou, shan tou,
Wang 's favorite places are:
bei jing, shang hai,
Process finished with exit code 0
6-10 喜欢的数字
fri_num={
'chen':[2,3,5],
'pei':[8,11,23],
'sun':[10,11,22],
'li':[12,34,44],
'zhao':[31,24,24,15],
}
for first,last in fri_num.items():
print(first.title(),"'s favorite numbers are:")
print(end='\t')
for x in last:
print(x,end=' ')
print('\n')
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
Chen 's favorite numbers are:
2 3 5
Pei 's favorite numbers are:
8 11 23
Sun 's favorite numbers are:
10 11 22
Li 's favorite numbers are:
12 34 44
Zhao 's favorite numbers are:
31 24 24 15
Process finished with exit code 0
6-11 城市:
citys={
'new york':{
'country':'The United States',
'population':'8 million',
'fact':'the Staples Center is in New York'
},
'houston':{
'country':'The United States',
'population':'2 million',
'fact':'the Toyota Center is in Houston'
},
'boston':{
'country':'The United States',
'population':'500 thousand',
'fact':'the TD Banknorth Garden is Boston',
},
}
for city in citys.items():
print(city[0].title(),': ')
for first,second in city[1].items():
print('\t',first,':',second)
print()
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
New York :
country : The United States
population : 8 million
fact : the Staples Center is in New York
Houston :
country : The United States
population : 2 million
fact : the Toyota Center is in Houston
Boston :
country : The United States
population : 500 thousand
fact : the TD Banknorth Garden is Boston
Process finished with exit code 0