当前位置:   article > 正文

《Python编程从入门到实践》第六章 字典知识点及习题_python字典习题

python字典习题

1.知识梳理

2.练习

  1. # 练习6.1
  2. Lihua = {'name': 'lihua', 'sex': 'female', 'first_name': 'Li', 'last_name': 'hua'}
  3. print(Lihua['name'])
  4. print(Lihua['sex'])
  5. print(Lihua['last_name'])
  6. # 练习6.2 喜欢的数字
  7. favorite_numbers={'mike': 1, 'jack': 2, 'tom': 3, }
  8. print(favorite_numbers['mike'])
  9. print(favorite_numbers['tom'])
  10. print(favorite_numbers['jack'])
  11. # 练习6.3
  12. languages = {'Python': 'easy', 'C': 'basic', 'C++': 'basic', 'matlab': 'mathematics',}
  13. language=languages['Python']
  14. print(f"Python's feature is {language}")
  15. for language in languages.keys():
  16. print(f"{language}:")
  17. print(f"{languages[language]}\n")
  1. # 练习6.4 词汇表2
  2. languages = {'Python': 'easy', 'C': 'basic', 'C++': 'basic', 'matlab': 'mathematics',} #创建字典
  3. languages['java'] = 'widely' #字典添加值
  4. print(languages)
  5. feature=languages.get('Python') #get()访问值
  6. print(feature)
  7. for k,v in languages.items(): #遍历所有键值对
  8. print(f"\n{k}")
  9. print(v)
  10. # 练习6.5 河流
  11. rivers={'nile':'egypt','Yangtze ':'China','Mississippi':'america'}
  12. print(rivers)
  13. for river ,country in rivers.items():
  14. print(f"The {river} runs though {country}")
  15. for river in rivers.keys():
  16. print(river)
  17. for country in rivers.values():
  18. print(country)
  19. # 练习6.6 调查
  20. languages = {'Python': 'easy', 'C': 'basic', 'C++': 'basic', 'matlab': 'mathematics',}
  21. lists = ['C', 'Java', 'Linux', 'matlab']
  22. for language in languages.keys():
  23. if language in lists:
  24. print(f"{language} in list")
  25. if language not in lists:
  26. print(f"{language} not in list")
  1. # 练习6.7 人们
  2. people = {
  3. 'Lihua' :{'name': 'lihua',
  4. 'sex': 'female',
  5. 'first_name': 'Li',
  6. 'last_name': 'hua',},
  7. 'Liming':{'name':'Liming',
  8. 'sex':'male',
  9. 'first_name':'Li',
  10. 'last_name':'ming',},
  11. 'Chenming':{'name':'Chenming',
  12. 'sex' : 'male',
  13. 'first_name':'Chen',
  14. 'last_name':'ming'},
  15. }
  16. print(people)
  17. # 练习6.8 宠物
  18. AA={'name':'AA',
  19. 'color':'white',
  20. 'age':3,
  21. 'owner':'Liming'}
  22. BB={'name':'BB',
  23. 'color':'grey',
  24. 'age':2,
  25. 'owner':'Lihua'}
  26. pets=[AA,BB]
  27. for pet in pets:
  28. print(pet)
  29. # 练习6.9喜欢的地方
  30. favorite_places={
  31. 'Lihua': ['XiAn','Beijing','Tianjin'],
  32. 'Liming': ['Huhot','Changsha','Guangzhou'],
  33. 'Zhangming': ['Xizang','Wulumuqi'],
  34. }
  35. for name,place in favorite_places.items():
  36. print(f"{name} likes {place}")
  37. # 练习6,10
  38. favorite_numbers={'mike': [1,2,3], 'jack': [4,5,6], 'tom': [7,8,9], }
  39. for name,number in favorite_numbers.items():
  40. print(f"{name}'s favourite numbers are {number}")
  41. # 练习 6.11
  42. cities={
  43. 'Beijing':{
  44. 'country':'China',
  45. 'fact':'capital',
  46. 'population':'large',},
  47. 'Washington':{'country':'America',
  48. 'fact':'the White Hous',
  49. 'population':'middle',}
  50. }
  51. print(cities)
  52. # 练习6.12 扩展
  53. cities['london'] = {'country':'England',
  54. 'fact':'Big Ben',
  55. 'population':'small'}
  56. print(cities)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/998139
推荐阅读
相关标签
  

闽ICP备14008679号