赞
踩
- # 练习6.1
- Lihua = {'name': 'lihua', 'sex': 'female', 'first_name': 'Li', 'last_name': 'hua'}
- print(Lihua['name'])
- print(Lihua['sex'])
- print(Lihua['last_name'])
-
- # 练习6.2 喜欢的数字
- favorite_numbers={'mike': 1, 'jack': 2, 'tom': 3, }
- print(favorite_numbers['mike'])
- print(favorite_numbers['tom'])
- print(favorite_numbers['jack'])
-
- # 练习6.3
- languages = {'Python': 'easy', 'C': 'basic', 'C++': 'basic', 'matlab': 'mathematics',}
- language=languages['Python']
- print(f"Python's feature is {language}")
- for language in languages.keys():
- print(f"{language}:")
- print(f"{languages[language]}\n")
- # 练习6.4 词汇表2
- languages = {'Python': 'easy', 'C': 'basic', 'C++': 'basic', 'matlab': 'mathematics',} #创建字典
- languages['java'] = 'widely' #字典添加值
- print(languages)
- feature=languages.get('Python') #get()访问值
- print(feature)
- for k,v in languages.items(): #遍历所有键值对
- print(f"\n{k}")
- print(v)
-
- # 练习6.5 河流
- rivers={'nile':'egypt','Yangtze ':'China','Mississippi':'america'}
- print(rivers)
- for river ,country in rivers.items():
- print(f"The {river} runs though {country}")
-
- for river in rivers.keys():
- print(river)
- for country in rivers.values():
- print(country)
-
-
- # 练习6.6 调查
- languages = {'Python': 'easy', 'C': 'basic', 'C++': 'basic', 'matlab': 'mathematics',}
- lists = ['C', 'Java', 'Linux', 'matlab']
- for language in languages.keys():
- if language in lists:
- print(f"{language} in list")
-
- if language not in lists:
- print(f"{language} not in list")
- # 练习6.7 人们
- people = {
- 'Lihua' :{'name': 'lihua',
- 'sex': 'female',
- 'first_name': 'Li',
- 'last_name': 'hua',},
- 'Liming':{'name':'Liming',
- 'sex':'male',
- 'first_name':'Li',
- 'last_name':'ming',},
- 'Chenming':{'name':'Chenming',
- 'sex' : 'male',
- 'first_name':'Chen',
- 'last_name':'ming'},
- }
- print(people)
-
- # 练习6.8 宠物
- AA={'name':'AA',
- 'color':'white',
- 'age':3,
- 'owner':'Liming'}
- BB={'name':'BB',
- 'color':'grey',
- 'age':2,
- 'owner':'Lihua'}
- pets=[AA,BB]
- for pet in pets:
- print(pet)
-
-
- # 练习6.9喜欢的地方
- favorite_places={
- 'Lihua': ['XiAn','Beijing','Tianjin'],
- 'Liming': ['Huhot','Changsha','Guangzhou'],
- 'Zhangming': ['Xizang','Wulumuqi'],
- }
- for name,place in favorite_places.items():
- print(f"{name} likes {place}")
-
- # 练习6,10
- favorite_numbers={'mike': [1,2,3], 'jack': [4,5,6], 'tom': [7,8,9], }
- for name,number in favorite_numbers.items():
- print(f"{name}'s favourite numbers are {number}")
-
- # 练习 6.11
-
- cities={
- 'Beijing':{
- 'country':'China',
- 'fact':'capital',
- 'population':'large',},
- 'Washington':{'country':'America',
- 'fact':'the White Hous',
- 'population':'middle',}
- }
- print(cities)
-
- # 练习6.12 扩展
- cities['london'] = {'country':'England',
- 'fact':'Big Ben',
- 'population':'small'}
-
- print(cities)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。