当前位置:   article > 正文

python基础-operating_list_python operating

python operating
  1. # 1.遍历整个列表
  2. magicians = ['alice', 'david', 'carolina']
  3. for magician in magicians:
  4. print(magician)
  5. for magician in magicians:
  6. print(magician.title() + ", that was a great trick!")
  7. print("I can't wait to see your next trick, " + magician.title() + ".\n")
  8. # 2.避免缩进错误
  9. # 2.1忘记缩进
  10. # magicians = ['alice', 'david', 'carolina']
  11. # for magician in magicians:
  12. # print(magician)
  13. # 2.2忘记缩进额外的代码行
  14. # for magician in magicians:
  15. # print(magician.title() + ", that was a great trick!") # 因为此行缩进,所以不报错
  16. # print("I can't wait to see your next trick, " + magician.title() + ".\n") # 最后一个magician为carolina
  17. # 2.3不必要的缩进
  18. # message = 'hello'
  19. # print(message)
  20. # 2.4遗漏了冒号
  21. # for magician in magicians
  22. # print(magician)
  23. # P50动手试一试
  24. # 4-1
  25. pizzas = ['A', 'B', 'C']
  26. for pizza in pizzas:
  27. print('I like %s pizza' % pizza)
  28. print('I really love pizza')
  29. # 4-2
  30. animals = ['A', 'B', 'C']
  31. for animal in animals:
  32. print('A %s would make a great pet.' % animal)
  33. print('Any of these animals would make a great pet!')
  34. # 3创建数值列表
  35. # 3.1使用range()函数,产生的结果:{x belongs with Z : lower <= x < upper}
  36. # 使用range()时,如果输出不符合预期,请尝试将指定的值加1或减1。
  37. for value in range(1, 5):
  38. print(value)
  39. # 3.2使用range()创建数字列表
  40. numbers = list(range(1, 5))
  41. print(numbers)
  42. # 打印1~10内的偶数:
  43. even_numbers = list(range(2, 11, 2))
  44. print(even_numbers)
  45. # 创建一个包含前10个整数的平方的列表
  46. squares = []
  47. for value in range(1, 11):
  48. # square = value ** 2 # 两个*表示乘方运算
  49. # squares.append(square)
  50. squares.append(value ** 2)
  51. print(squares)
  52. # 3.3对数字列表执行统计计算
  53. digits = list(range(0, 10))
  54. print(digits)
  55. print(min(digits))
  56. print(max(digits))
  57. print(sum(digits))
  58. # 3.4列表解析:列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。
  59. '''
  60. 首先指定一个描述性的列表名,如squares; 然后,指定一个左方括号,
  61. 并定义一个表达式,用于生成你要存储到列表中的值。在这个示例中,表达式为value**2,
  62. 它计算平方值。接下来,编写一个for循环, 用于给表达式提供值,再加上右方括号。
  63. '''
  64. squares = [value**2 for value in range(1, 11)]
  65. print(squares)
  66. # P54动手试一试
  67. # 4-3
  68. for i in range(1, 21):
  69. print(i)
  70. # 4-4
  71. # for i in range(1, 1000001):
  72. # print(i)
  73. # 4-5
  74. numbers = list(range(1, 1000001))
  75. print(min(numbers))
  76. print(max(numbers))
  77. print(sum(numbers))
  78. # 4-6
  79. odds = list(range(1, 20, 2))
  80. for odd in odds:
  81. print(odd)
  82. # 4-7
  83. threes = []
  84. for i in range(3, 30):
  85. if i % 3 == 0:
  86. threes.append(i)
  87. print(threes)
  88. # 4-8/9
  89. nums = [value ** 3 for value in range(1, 11)]
  90. print(nums)
  91. # 4.使用列表的一部分
  92. # 4.1切片,要创建切片,可指定要使用的第一个元素和最后一个元素的索引。
  93. players = ['charles', 'martina', 'michael', 'florence', 'eli']
  94. print(players)
  95. print(players[0:3]) # 前三个
  96. print(players[1:4]) # 第2-4个
  97. # 如果你没有指定第一个索引或最后一个索引, Python将自动从列表开头开始或结尾终止:
  98. print(players[:4])
  99. print(players[1:])
  100. print(players[-3:]) # 后三个
  101. print(players[-2:]) # 后两个
  102. # 4.2遍历切片,如果要遍历列表的部分元素,可在for循环中使用切片。
  103. for player in players[:3]:
  104. print(player) # 前3个
  105. # 4.3复制列表
  106. my_foods = ['pizza', 'falafel', 'carrot cake'] #
  107. friend_foods = my_foods[:] # 在不指定任何索引的情况下从列表my_foods中提取一个切片,复制一个切片
  108. # friend_foods = my_foods # 浅复制,指向同一个列表
  109. print(friend_foods)
  110. my_foods.append('a')
  111. print('确实是两个列表')
  112. print(my_foods)
  113. print(friend_foods)
  114. # P58动手试一试
  115. # 4-10
  116. print('the list is:',players)
  117. print('The first three items in the list are:', players[:3])
  118. print('Three items from the middle of the list are:', players[1:4])
  119. print('The last three items in the list are:', players[-3:])
  120. # 4-11
  121. friend_pizzas = pizzas[:]
  122. print(friend_pizzas)
  123. print(pizzas)
  124. pizzas.append('D')
  125. friend_pizzas.append('F')
  126. print('My favorite pizzas are:')
  127. for pizza in pizzas:
  128. print(pizza)
  129. print("My friend's favorite pizza are:")
  130. for pizza in friend_pizzas:
  131. print(pizza)
  132. # 5.元组
  133. # Python将不能修改的值称为不可变的,而不可变的列表被称为元组。使用圆括号来标识
  134. # 5.1定义元祖
  135. dimensions = (200, 50)
  136. print(dimensions)
  137. # 5.2遍历元组中的所有值
  138. for dimension in dimensions:
  139. print(dimension)
  140. # 5.3修改元组变量,虽然不能修改元组的元素,但可以给存储元组的变量赋值
  141. dimensions = (300, 400)
  142. print(dimensions)
  143. # P60动手试一试
  144. #4-13
  145. foods = ('A', 'B', 'C', 'D', 'E')
  146. for food in foods:
  147. print(food)
  148. # foods[0] = 'C'
  149. foods = ('X', 'Y', 'C', 'D', 'E')
  150. for food in foods:
  151. print(food)
  152. # 6.设置代码格式:PEP8格式指南

 

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

闽ICP备14008679号