赞
踩
- # 6-1 人
- ElonMusk= {'first_name':'Elon', 'last_name':'Musk', 'age':'53', 'city':'Pretoria'}
-
- for key,value in ElonMusk.items():
- print(key, ':', value)
-
- # 6-2 喜欢的数字
- favorite_number = {
- 'Tesla':'4',
- 'BMW':'2',
- 'NIO':'1',
- 'Huawei':'6',
- 'BYD':'8',
- }
-
- for k,v in favorite_number.items():
- print(k + "'s favorite number is " + v)
-
- # 6-3 词汇表
- dics = {
- 'int': '整形',
- 'bool': '布尔类型',
- 'float': '浮点型',
- 'str': '字符串',
- 'list': '列表',
- }
- print("int" + ":" + dics["int"])
- print("bool" + ":" + dics["bool"])
- print("float" + ":" + dics["float"])
- print("str" + ":" + dics["str"])
- print("list" + ":" + dics["list"])
-
- # 6-4 词汇表2
- dics = {
- 'int': '整形',
- 'bool': '布尔类型',
- 'float': '浮点型',
- 'str': '字符串',
- 'list': '列表',
- }
- for k,v in dics.items():
- print(k + ':' + v)
-
- # 6-5 河流
- rivers = {
- "ChangJiang":"China",
- "Nile":"Egypt",
- "Amazon":"Brazil",
- }
- for k,v in rivers.items():
- print(f"The {k} runs through {v}")
- for k in rivers.keys():
- print(k)
- for v in rivers.values():
- print(v)
-
- # 6-6 调查
- favorite_languages = {
- 'jen':'python',
- 'sarah':'c',
- 'edward':'ruby',
- 'phil':'python',
- }
-
- names = ['Musk', 'sarah', 'phil', 'LiMing']
-
- lists = []
- for k in favorite_languages.keys():
- lists.append(k)
-
- for name in names:
- if name in lists:
- print(f"{name}:Thank You!")
- else:
- print(f"{name}:Invite!")
-
- # 6-7 人
- ElonMusk= {'first_name':'Elon', 'last_name':'Musk', 'age':'53', 'city':'Pretoria'}
- MaYun= {'first_name':'Yun', 'last_name':'Ma', 'age':'60', 'city':'HangZhou'}
- WangChuanfu= {'first_name':'Chuanfu', 'last_name':'Wang', 'age':'58', 'city':'ShenZhen'}
-
- peoples = [ElonMusk, MaYun, WangChuanfu]
- for people in peoples:
- print(people)
-
- # 6-8 宠物
- feifei = {'type':'dog', 'master':'alien'}
- little_cute = {'type':'cat', 'master':'alice'}
- huahua = {'type':'panda', 'master':'tan'}
-
- pets = [feifei, little_cute, huahua]
- for pet in pets:
- print(pet)
-
- # 6-9 喜欢的地方
- favorite_places = {
- 'feifei':['Beijing', 'SuZhou'],
- 'Bob':['ShangHai','HangZhou', 'ShenZhen'],
- 'huahua':['Chengdu', 'ChongQing'],
- }
- for fp_k,fp_v in favorite_places.items():
- print(f"\n{fp_k}'s favorite places are:" )
- for v in fp_v: # 注意下
- print(v)
-
- # 6-10 喜欢的数字
- favorite_number = {
- 'Tesla':['4','5','6'],
- 'BMW':['5','6','7'],
- 'NIO':['1','2','3'],
- 'Huawei':['2','3','4'],
- 'BYD':['3','4','5'],
- }
-
- for fn_k,fn_v in favorite_number.items():
- print(f"\n{fn_k}'s favorite numbers are:")
- for v in fn_v:
- print(v)
-
- # 6-11 喜欢的数字
- citys = {
- 'ChengDu':{
- 'country': 'China',
- 'population': '1200 0000',
- 'fact': 'panda'
- },
- 'ShangHai':{
- 'country': 'China',
- 'population': '2300 0000',
- 'fact': 'The Bund'
- },
- 'LosAngeles': {
- 'country': 'America',
- 'population': '400 0000',
- 'fact': 'Hollywood'
- }
- } # 字典中嵌套了三个字典
-
- for k,v in citys.items():
- print(f"\n{k}:")
- for k1,v1 in v.items():
- print(f"\t{k1}:{v1}")
-
- # 6-12 拓展
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。