当前位置:   article > 正文

python编程 从入门到实践 第六章 动手试一试参考答案 含运行结果-字典_person编程从入门到实战动手试一试

person编程从入门到实战动手试一试

坚持,共勉!

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #6-1
  4. print("6-1")
  5. persons = {'first_name':'congcong',
  6. 'last_name':'yang',
  7. 'age':18,
  8. 'city':'guangzhou'
  9. }
  10. print(persons)
  11. print(persons['first_name'])
  12. print(persons['last_name'])
  13. print(persons['age'])
  14. print(persons['city'])
  15. #6-2
  16. print('\n6-2')
  17. love_number = {'bibi':1,
  18. 'congcong':9,
  19. 'wendy':8,
  20. 'xiaobobo':2,
  21. 'zhen':7}
  22. print(love_number)
  23. #6-3
  24. print('\n6-3')
  25. glossarys = {'class':'类',
  26. 'object':'对象',
  27. 'package':'包',
  28. 'value':'值',
  29. 'define':'定义'
  30. }
  31. print('class:'+ glossarys['class'])
  32. print('object:'+glossarys['object'])
  33. print('package:' + glossarys['package'])
  34. print('value' + glossarys['value'])
  35. print('define' + glossarys['define'])
  36. print('class:' + "\n" + glossarys['class'])
  37. print('object:' + '\n' + glossarys['object'])
  38. print('package:' + '\n' + glossarys['package'])
  39. print('value:' + '\n' + glossarys['value'])
  40. print('define:' + '\n' + glossarys['define'])
  41. #6-4
  42. print("6-4")
  43. glossarys['abstract base class'] = '抽象基类'
  44. glossarys['annotation'] = '注解'
  45. glossarys['argument'] = '参数'
  46. glossarys['asynchronous generator'] = '异步生成器'
  47. glossarys['asynchronous generator iterator'] = '异步生成器迭代器'
  48. for key,value in glossarys.items():
  49. print("\nkey: " + key)
  50. print("\nvalue: " + value)
  51. #6-5
  52. #打印key和value值
  53. print('\n6-5')
  54. three_big_river = {'nile':'egypt','changjiang':'china','amazon':'south america'}
  55. for river,country in three_big_river.items():
  56. print('\nThe ' + river + ' runs through' + country)
  57. #打印key值
  58. for river in three_big_river.keys():
  59. print(river)
  60. #打印value值
  61. for country in three_big_river.values():
  62. print(country)
'
运行

 

  1. #6-6
  2. print('6-6')
  3. favorite_languages = {
  4. 'jen': 'python',
  5. 'sarah': 'C',
  6. 'edward': 'ruby',
  7. 'phil': 'python'
  8. }
  9. #for name,language in favorite_languages.items():
  10. #print(name.title() + "'s favorite language is " + language.title() + ".")
  11. persons = ['bobo','congcong','jen','sarah']
  12. for name in persons:
  13. #print(name.title())
  14. if name not in favorite_languages:
  15. print (name + ' please take our poll!')
  16. else:
  17. print(name + ' thany you for taking the poll!')
'
运行

 

  1. #6-7
  2. print("6-7")
  3. persons = {'first_name':'congcong',
  4. 'last_name':'yang',
  5. 'age':18,
  6. 'city':'guangzhou'
  7. }
  8. persons_1 = {'first_name':'bobo',
  9. 'last_name':'yang',
  10. 'age':19,
  11. 'city':'guangzhou'
  12. }
  13. persons_2 = {'first':'xiaobobo',
  14. 'last_name':'yang',
  15. 'age':20,
  16. 'city':'guangzhou'
  17. }
  18. people = [persons,persons_1,persons_2]
  19. for person in people:
  20. print(person)
'
运行

 

  1. #6-8
  2. print('6-8')
  3. wangcai = {'dog':'kind',
  4. 'clever':'owner'
  5. }
  6. xiaoyu ={'fish': 'kind',
  7. 'bobo':'owner'
  8. }
  9. xiaohong = {'cat':'kind',
  10. 'xiaobobo':'owner'
  11. }
  12. pets = [wangcai,xiaoyu,xiaohong]
  13. for pet in pets:
  14. print(pet)
  15. #6-9 喜欢的地方,字典中包含列表
  16. print('\n6-9')
  17. favorite_places = {'bobos':['guangzhou','hangzhou'],
  18. 'yang':['sh'],#记得加[],啊,我一开始就是忘记了加[],导致if语句不成立
  19. 'cong':['beijing','zhuhai'],
  20. }
  21. for name,places in favorite_places.items():
  22. #print(len(places))
  23. if len(places) >1 :
  24. print('\n' + name.title() + "'s favoriate places are:")
  25. for place in places:
  26. print ('\t' + place.title())
  27. else:
  28. print('\n' + name.title() +"'s favoriate place is:")
  29. print('\t' + place.title())
  30. #6-10
  31. #6-2
  32. #print('\n6-2')
  33. love_number = {'bibi':1,
  34. 'congcong':9,
  35. 'wendy':8,
  36. 'xiaobobo':2,
  37. 'zhen':7}
  38. print(love_number)
  39. print('\n6-10')
  40. love_numbers = {'bibi':[1,2,3,4],
  41. 'congcong':[9,1,2],
  42. 'wendy':[8,3],
  43. 'xiaobobo':[2,3],
  44. 'zhen':[7,1]
  45. }
  46. for name,numbers in love_numbers.items():
  47. #print(numbers)
  48. print('\n' +name.title() + ":")
  49. for number in numbers:
  50. print(number)
  51. #6-11城市
  52. print("\n6-11")
  53. cities = {
  54. 'guangzhou': {
  55. 'country':'china',
  56. 'population':'13billion',
  57. 'fact': 'beatiful city',
  58. },
  59. 'shenzhen' : {
  60. 'country' : 'china',
  61. 'population' : '13billion',
  62. 'fact' : 'near sea',
  63. },
  64. "shanghai": {
  65. 'country' : 'china',
  66. 'population': '13billion',
  67. 'fact' : 'big',
  68. }
  69. }
  70. for city,info in cities.items():
  71. print("\ncity:" + city)
  72. country = info['country']
  73. population = info['population']
  74. fact = info['fact']
  75. print('\tpopulation:' + population.title())
  76. print('\tfact:' + fact.title())
'
运行

 

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

闽ICP备14008679号