遍历整个列表:
name = ['yang','li','mu','huan','xu','hu','deng']
for names in name: (遍历列表name的所有值,然后保存到变量names中)
print(names)
创建数值列表:
使用函数range(). Python函数 range() 让你能够轻松地生成一系列的数字.
for value in range(1, 5)。( 打印的结果是 1,2,3,4)
使用 range() 创建数字列表:
要创建数字列表,可使用函数 list() 将 range() 的结果直接转换为列表。如果将 range() 作为list() 的参数,输出将为一个数字列表。
numbers =list(range(1,6))\
打印1-10内的偶数
even_numbers = list(range(2,11,2)) 解释: 函数 range() 从2开始数,然后不断地加2,直到达到或超过终值(11)
print(even_numbers)
min()取列表最小值,max()取列表最大值
切片:
要创建切片,可指定要使用的第一个元素和最后一个元素的索引
如果提取列表的第2~4个元素,可将起始索引指定为 1 ,并将终止索引指定为 4 :
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
遍历切片:
要遍历列表的部分元素,可在 for 循环中使用切片。例,我们遍历前三名队员
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
元组: