坚持,共勉!
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#6-1
print("6-1")
persons = {'first_name':'congcong',
'last_name':'yang',
'age':18,
'city':'guangzhou'
}
print(persons)
print(persons['first_name'])
print(persons['last_name'])
print(persons['age'])
print(persons['city'])
#6-2
print('\n6-2')
love_number = {'bibi':1,
'congcong':9,
'wendy':8,
'xiaobobo':2,
'zhen':7}
print(love_number)
#6-3
print('\n6-3')
glossarys = {'class':'类',
'object':'对象',
'package':'包',
'value':'值',
'define':'定义'
}
print('class:'+ glossarys['class'])
print('object:'+glossarys['object'])
print('package:' + glossarys['package'])
print('value' + glossarys['value'])
print('define' + glossarys['define'])
print('class:' + "\n" + glossarys['class'])
print('object:' + '\n' + glossarys['object'])
print('package:' + '\n' + glossarys['package'])
print('value:' + '\n' + glossarys['value'])
print('define:' + '\n' + glossarys['define'])
#6-4
print("6-4")
glossarys['abstract base class'] = '抽象基类'
glossarys['annotation'] = '注解'
glossarys['argument'] = '参数'
glossarys['asynchronous generator'] = '异步生成器'
glossarys['asynchronous generator iterator'] = '异步生成器迭代器'
for key,value in glossarys.items():
print("\nkey: " + key)
print("\nvalue: " + value)
#6-5
#打印key和value值
print('\n6-5')
three_big_river = {'nile':'egypt','changjiang':'china','amazon':'south america'}
for river,country in three_big_river.items():
print('\nThe ' + river + ' runs through' + country)
#打印key值
for river in three_big_river.keys():
print(river)
#打印value值
for country in three_big_river.values():
print(country)
#6-6
print('6-6')
favorite_languages = {
'jen': 'python',
'sarah': 'C',
'edward': 'ruby',
'phil': 'python'
}
#for name,language in favorite_languages.items():
#print(name.title() + "'s favorite language is " + language.title() + ".")
persons = ['bobo','congcong','jen','sarah']
for name in persons:
#print(name.title())
if name not in favorite_languages:
print (name + ' please take our poll!')
else:
print(name + ' thany you for taking the poll!')
#6-7
print("6-7")
persons = {'first_name':'congcong',
'last_name':'yang',
'age':18,
'city':'guangzhou'
}
persons_1 = {'first_name':'bobo',
'last_name':'yang',
'age':19,
'city':'guangzhou'
}
persons_2 = {'first':'xiaobobo',
'last_name':'yang',
'age':20,
'city':'guangzhou'
}
people = [persons,persons_1,persons_2]
for person in people:
print(person)
#6-8
print('6-8')
wangcai = {'dog':'kind',
'clever':'owner'
}
xiaoyu ={'fish': 'kind',
'bobo':'owner'
}
xiaohong = {'cat':'kind',
'xiaobobo':'owner'
}
pets = [wangcai,xiaoyu,xiaohong]
for pet in pets:
print(pet)
#6-9 喜欢的地方,字典中包含列表
print('\n6-9')
favorite_places = {'bobos':['guangzhou','hangzhou'],
'yang':['sh'],#记得加[],啊,我一开始就是忘记了加[],导致if语句不成立
'cong':['beijing','zhuhai'],
}
for name,places in favorite_places.items():
#print(len(places))
if len(places) >1 :
print('\n' + name.title() + "'s favoriate places are:")
for place in places:
print ('\t' + place.title())
else:
print('\n' + name.title() +"'s favoriate place is:")
print('\t' + place.title())
#6-10
#6-2
#print('\n6-2')
love_number = {'bibi':1,
'congcong':9,
'wendy':8,
'xiaobobo':2,
'zhen':7}
print(love_number)
print('\n6-10')
love_numbers = {'bibi':[1,2,3,4],
'congcong':[9,1,2],
'wendy':[8,3],
'xiaobobo':[2,3],
'zhen':[7,1]
}
for name,numbers in love_numbers.items():
#print(numbers)
print('\n' +name.title() + ":")
for number in numbers:
print(number)
#6-11城市
print("\n6-11")
cities = {
'guangzhou': {
'country':'china',
'population':'13billion',
'fact': 'beatiful city',
},
'shenzhen' : {
'country' : 'china',
'population' : '13billion',
'fact' : 'near sea',
},
"shanghai": {
'country' : 'china',
'population': '13billion',
'fact' : 'big',
}
}
for city,info in cities.items():
print("\ncity:" + city)
country = info['country']
population = info['population']
fact = info['fact']
print('\tpopulation:' + population.title())
print('\tfact:' + fact.title())