《Python编程:从入门到实践》答案

川长思鸟来 2023-02-21 11:48 75阅读 0赞

快乐又不要钱,为什么不呢?

【注】本文中所有的代码本人全部测试通过后才写在下面。考虑到夹杂结果图片太乱,所以没加。大家复制代码拿去运行即可。

前面章节的答案后续会补上。

【注】所有包含中文的代码都必须把编码设置成UTF-8才能正常运行。我用的是Geany,设置方法为:文档——设置文件编码——Unicode-——UTF-8

持续更新……

第二章

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70

  1. print("Hello my new life,\nhllo ,the technology world.\nplese be gentle to me.")

2-2

  1. message="Hello my new life,\nhllo ,the technology world.\nplese be gentle to me."
  2. print(message)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 1 2-3

  1. name = 'Eric'
  2. print('Hello '+name+', would you like to learn some pyghon today?')

2-4

  1. name = 'eric'
  2. print(name.title())
  3. print(name.upper())
  4. print(name.lower())

  1. name = 'albert einstein'
  2. saying="A person who never made a mistake never tried anything new."
  3. print(name.title()+"once said"+": "+'\n\t"'+saying+'"')

20200905171002347.png

  1. name = ' albert einstein '
  2. saying="A person who never made a mistake never tried anything new."
  3. print(name.title())
  4. print(name.title().lstrip().rstrip()+"once said"+": "+'\n\t"'+saying+'"')
  5. print(name.strip())

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 2

  1. print(80/10)
  2. print(2*4)
  3. print(10-2)
  4. number=4
  5. print("my favorite number is "+str(number))

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 3

第三章

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 4

  1. names=['王辰','王杰','许博文','肖嘉良','田家铭','徐汉章']
  2. for name in names:
  3. print(name)
  4. names=['王辰','王杰','许博文','肖嘉良','田家铭','徐汉章']
  5. for name in names:
  6. print('Hello '+name+" it's good to see you !")
  7. names=['car','bicycle','shared bikes','motorcycle','bus']
  8. for name in names:
  9. print('To be the truth ,my favorite vehicle is '+name+'.')

2020090609562849.png

  1. names=['泽东','和珅','胡雪岩','华罗庚','王羲之']
  2. for name in names:
  3. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 5

  1. names=['泽东','和珅','胡雪岩','华罗庚','王羲之']
  2. for name in names:
  3. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')
  4. print('\n\nSorry the '+names[-2]+' has no time these days ,please call someone else.')
  5. names[-2]='张三丰'
  6. print(names)
  7. for name in names:
  8. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 6

20200906110911290.png

  1. names=['泽东','和珅','胡雪岩','华罗庚','王羲之']
  2. for name in names:
  3. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')
  4. print('\n\nSorry the '+names[-2]+' has no time these days ,please call someone else.')
  5. names[-2]='张三丰'
  6. print(names)
  7. for name in names:
  8. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')
  9. print('My lovely friends ,i have find a bigger desk and now we can invite more friends')
  10. print(names)
  11. print('\n')
  12. names.append('老子')
  13. names.insert(0,'水阡陌')
  14. for name in names:
  15. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 7

  1. names=['泽东','和珅','胡雪岩','华罗庚','王羲之']
  2. for name in names:
  3. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')
  4. print('\n\nSorry the '+names[-2]+' has no time these days ,please call someone else.')
  5. names[-2]='张三丰'
  6. print(names)
  7. for name in names:
  8. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')
  9. print('My lovely friends ,i have find a bigger desk and now we can invite more friends')
  10. print(names)
  11. print('\n')
  12. names.append('老子')
  13. names.insert(0,'水阡陌')
  14. for name in names:
  15. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')
  16. print(names)
  17. print('Oh my God ,now i have to pick two of you \n')
  18. for index in range(2,len(names)):
  19. pop_name=names.pop()
  20. print("sorry "+pop_name+" i can't help it ,because the book says.")
  21. for name in names:
  22. print('Dear '+name+ ' everything went according to plan.')
  23. del names[-1]
  24. del names[0]
  25. print(names)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 8

  1. tourist_attractions=['shenzhen','guangzhou','shanghai','jinlinqu']
  2. print(tourist_attractions)
  3. print(sorted(tourist_attractions))
  4. print(tourist_attractions)
  5. print(sorted(tourist_attractions,reverse=True))

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 9

  1. tourist_attractions=['shenzhen','guangzhou','shanghai','jinlinqu']
  2. print(tourist_attractions)
  3. print(sorted(tourist_attractions))
  4. print(tourist_attractions)
  5. print(sorted(tourist_attractions,reverse=True))
  6. print(tourist_attractions)
  7. tourist_attractions.reverse()
  8. print(tourist_attractions)
  9. tourist_attractions.reverse()
  10. print(tourist_attractions)
  11. tourist_attractions.sort()
  12. print(tourist_attractions)
  13. tourist_attractions.sort(reverse=True)
  14. print(tourist_attractions)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 10

  1. names=['毛泽东','和珅','胡雪岩','华罗庚','王羲之']
  2. for name in names:
  3. print('Dear '+name+' ,i respectfully invite you to join me for dinner.')
  4. print('\n\nSorry the '+names[-2]+' has no time these days ,please call someone else.')
  5. names[-2]='张三丰'
  6. print(names)
  7. print(len(names))

第四章

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 11

  1. pizzas=['zebra','peacock','panda']
  2. for pizza in pizzas:
  3. print('My favorite pizza is '+pizza+'.')
  4. print('I really like pizza.')

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 12

  1. animals=['zebra','peacock','panda']
  2. for animal in animals:
  3. print(animal+' in China is the protected-animals.')
  4. print('They are the part of this world ,in other words ,it is our friends.')

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 13

  1. #4-3
  2. for number in range(1,21):
  3. print(number)
  4. print('\n'+'=====================')
  5. #4-4
  6. #for num in range(1,1000001):
  7. # print(num)
  8. #4-5
  9. number=list(range(1,1000001))
  10. print(min(number))
  11. print(max(number))
  12. print(sum(number))
  13. print('\n'+'=====================')
  14. #4-6
  15. odds=list(range(1,21,2))
  16. for odd in odds:
  17. print(odd)
  18. print('\n'+'=====================')
  19. #4-7
  20. three_multiple=[num*3 for num in range(1,31)]
  21. for number in three_multiple:
  22. print(number)
  23. print('\n'+'=====================')
  24. #4-8
  25. cubes=[num**3 for num in range(1,11)]
  26. for cube in cubes:
  27. print(cube)
  28. print('\n'+'=====================')
  29. #4-9同4-8

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 14

  1. pizzas=['zebra','peacock','panda','shark','dolphin','eagle']
  2. print('The first three items in this list is: ')
  3. print(pizzas[:3])
  4. print('The items from the middle of list is: ')
  5. print(pizzas[1:4])
  6. print('The last three items in this list is: ')
  7. print(pizzas[-3:])

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 15

  1. pizzas=['zebra','peacock','panda','shark','dolphin','eagle']
  2. print('The first three items in this list is: ')
  3. print(pizzas[:3])
  4. print('The items from the middle of list is: ')
  5. print(pizzas[1:4])
  6. print('The last three items in this list is: ')
  7. print(pizzas[-3:])
  8. #4-10
  9. friend_pizzas=pizzas[:]
  10. friend_pizzas.append('bear')
  11. print("My favorite pizza is:\n")
  12. for pizza in pizzas:
  13. print(pizza)
  14. print('++++++++++++++')
  15. print("My friend favorite pizza is :\n")
  16. for pizza in friend_pizzas:
  17. print(pizza)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 16

  1. foods=('steamed stuffed bun','steamed bun','dumplings','congee','rice')
  2. for food in foods:
  3. print(food)
  4. print('\n')
  5. #foods[0]='rice'
  6. foods=('noodle','hot pot','dumplings','congee','rice')
  7. for food in foods:
  8. print(food)

第五章

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 17

  1. car='subaru'
  2. print("Is car=='subaru'? I predict True.")
  3. print(car=='subaru')
  4. print("\nIs car =='audi'? Ipredict False.")
  5. print(car=='audi')
  6. animal='peacock'
  7. print(animal=='zebra')

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 18

  1. yesterday_you_said="you are my only lover forever."
  2. Today_you_say="you can't control who I really love."
  3. print(yesterday_you_said==Today_you_say)
  4. print('\n')
  5. your_dream="Success"
  6. situation="success"
  7. print(your_dream.lower()==situation)
  8. print(34<=4)
  9. if 34<45 and 100*2<=400:
  10. print("the computer is better than child.")
  11. new_words=['peacock','cube','eagle']
  12. if 'peacock' in new_words:
  13. print("you can figure it!")
  14. if 'bear' not in new_words:
  15. print("take care yur eyes!")

20200907143552237.png

20200907143611364.png

  1. alien_color='green'
  2. if alien_color=='green':
  3. print("you get 5 points")
  4. alien_color='red'
  5. if alien_color=='green':
  6. print("you get 5 points")

20200907143859899.png

  1. alien_color='red'
  2. if alien_color=='green':
  3. print("you get 5 points")
  4. else:
  5. print("you get 10 points")

20200907144433386.png

  1. alien_color='green'
  2. if alien_color=='greaen':
  3. print("you get 5 points")
  4. elif alien_color=='red':
  5. print("you get 10 points")
  6. else :
  7. print("you get 15 points")

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 19

  1. age=23
  2. if age<2:
  3. print("U are an infant.")
  4. elif age<4:
  5. print("U are a baby")
  6. elif age<13:
  7. print("U are a child.")
  8. elif age<20:
  9. print("U are a youngth.")
  10. elif age<65:
  11. print("U are an adult.")
  12. else :
  13. print("U are an elder.")

20200907144958751.png

  1. fruit=['orange','apple','graph','banana']
  2. if 'apple' in fruit:
  3. print("you like apple.")
  4. if 'graph' in fruit:
  5. print("you like graph.")
  6. if 'watermellion' not in fruit:
  7. print('you do not like watermallion')

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 20

  1. guests=['admin','A','W','L','Z']
  2. for guest in guests:
  3. if guest=='admin' :
  4. print("love you my admin~~")
  5. else :
  6. print("welcome home "+guest +".")

20200630122155206.png

  1. guests=[]
  2. if guests:
  3. for guest in guests:
  4. if guest=='admin' :
  5. print("love you my admin~~")
  6. else :
  7. print("welcome home "+guest +".")
  8. else:
  9. print("We need to find some users!")

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 21

  1. current_users=['A','B','C','D','E']
  2. new_users=['A','B','X','Y','S']
  3. for new_user in new_users:
  4. if new_user in current_users:
  5. print('Sorry ,you need to change your name!')
  6. else:
  7. print('This name has never been used!')

20200630124917599.png

【做这一问时遇到了问题,我先把正解写在下面再把我错误那个 拿出来分析】

  1. #这是正解
  2. current_users=['A','B','C','D','E']
  3. curren_users=[x.swapcase() for x in current_users]
  4. new_users=['A','B','X','S','Y']
  5. for user in new_users:
  6. if user.lower() in current_users:
  7. print('Sorry ,this name has been used ')
  8. else :
  9. print('Ok ,you have a new name')
  10. #这是有问题的代码
  11. current_users=['A','B','C','D','E']
  12. #我主要错在这个for 循环,我想把列表中所有元素变小写。
  13. for current_user in current_users:
  14. store=current_user.lower()
  15. current_users.append(store)
  16. current_users.remove(store)
  17. print(current_users)
  18. new_users=['a','B','X','Y','S']
  19. for user in new_users:
  20. if user.lower() in current_users:
  21. print('Sorry ,you need to change your name!')
  22. else:
  23. print('This name has never been used!')
  24. 错误分析:
  25. 就是,for 循环是始终按索引+1去遍历,在删除了第一个元素之后,我想要遍历的第二个元素变成了第一个元素,
  26. 但此时for 循环认为已经遍历完了第一个,开始遍历第二个。此时的第二个就是没删除元素时的第三个元素。所以我看到的是跳了一个的结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 22

  1. nums=[1,2,3,4,5,6,7,8,9]
  2. for num in nums:
  3. if num==1:
  4. print(str(num)+'st')
  5. elif num==2:
  6. print(str(num)+'nd')
  7. elif num==3:
  8. print(str(num)+'rd')
  9. else:
  10. print(str(num)+'th')

第六章

20200711102823296.png

  1. acquaintance={
  2. 'first_name':'zhang',
  3. 'last_name':'shuyu',
  4. 'city':'heilongjiang',
  5. }
  6. print(acquaintance['first_name'])
  7. print(acquaintance['last_name'])
  8. print(acquaintance['city'])

20200711103111389.png

  1. favorate_num={
  2. 'me':'2',
  3. 'she':'3',
  4. 'he':'8',
  5. }
  6. print(favorate_num['she'])

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 23

  1. words={
  2. 'str':'change the number to string',
  3. 'list':'to form a list',
  4. 'lstrip':'delete the left blank in the string',
  5. 'range':'to form a series of number between '+
  6. 'the first number and the end number',
  7. 'swapcase':'change the upper to lower or reverse',
  8. }
  9. print('str'+': '+words['str']+'\n')
  10. print('list'+': '+words['list']+'\n')
  11. print('lstrip'+': '+words['lstrip']+'\n')
  12. print('range'+': '+words['range']+'\n')
  13. print('swapcase'+': ' +words['swapcase']+'\n')

20200711155032609.png

  1. rivers={
  2. 'Rhine':'Switzerland',
  3. 'changJiang':'china',
  4. 'nile':'egypt',
  5. }
  6. for river,country in rivers.items():
  7. print('The '+river.title()+'runs through the '+country.title()+'.')
  8. #这道题需要注意的地方是 for 循环的用法,其中,rivers.items()不要忘了items()

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NodXl1ZGV4aWFvd3U_size_16_color_FFFFFF_t_70 24

  1. favorate_languages={
  2. 'A':'english',
  3. 'C':'chinese',
  4. 'D':'python',
  5. 'X':'ahahha',
  6. }
  7. inquiry_list=['A','C','D','W']
  8. for name in inquiry_list:
  9. if name in favorate_languages.keys():
  10. print('you need to have a life you like and meet the lover one for you')
  11. else:
  12. print('if there is a mess, change it')

202007121141220.png

  1. acquaintance_1={
  2. 'first_name':'zhang',
  3. 'last_name':'shuyu',
  4. 'city':'heilongjiang',
  5. }
  6. acquaintance_1={
  7. 'first_name':'zhang',
  8. 'last_name':'shuyu',
  9. 'city':'heilongjiang',
  10. }
  11. acquaintance_2={
  12. 'first_name':'liu',
  13. 'last_name':'fang',
  14. 'city':'dalian',
  15. }
  16. acquaintance_3={
  17. 'first_name':'cui',
  18. 'last_name':'shijie',
  19. 'city':'hubei',
  20. }
  21. people=[acquaintance_1,acquaintance_2,acquaintance_3]
  22. for person in people:
  23. print(person)

20200712114236101.png

  1. cat={
  2. 'category':'bosi',
  3. 'lord':'fangfang'
  4. }
  5. dog={
  6. 'category':'dubin',
  7. 'lord':'haha',
  8. }
  9. rabbit={
  10. 'category':'white',
  11. 'lord':'dadada'
  12. }
  13. pets=[cat,dog,rabbit]
  14. for pet in pets:
  15. print(pet)

20200712123241873.png

  1. favorate_places={
  2. 'me':['changjiang','huanghe','shaolinsi'],
  3. 'she':['harBin','jilin','nanjing'],
  4. 'he':['beside his home a yard']
  5. }
  6. for man,places in favorate_places.items():
  7. print(man+': ')
  8. for place in places:
  9. print(place)
  10. print('\n')

20200712123908817.png

  1. favorate_num={
  2. 'me':['2','5','8'],
  3. 'she':['3','6','9'],
  4. 'he':['7','4','1'],
  5. }
  6. for name,number in favorate_num.items():
  7. print(name+'\'s favorate number are :')
  8. for num in number:
  9. print(num)
  10. print('\n')

20200712124514800.png

  1. cities={
  2. 'luoyang':{
  3. 'belong':'china',
  4. 'population':'1000000',
  5. 'fact':'the peony\'s hometown',
  6. },
  7. 'paris':{
  8. 'belong':'france',
  9. 'population':'2000',
  10. 'fact':'Notre Dame de Paris was set on fire'
  11. },
  12. 'moscow':{
  13. 'belong':'Russia',
  14. 'population':'34000',
  15. 'fact':'fierce fighting people',
  16. }
  17. }
  18. for city,info in cities.items():
  19. print(city.title()+':'+'\n')
  20. for attribute,detail in info.items():
  21. print(attribute.title()+': '+detail.title())

发表评论

表情:
评论列表 (有 0 条评论,75人围观)

还没有评论,来说两句吧...

相关阅读