1 遍历整个列表
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print (magician)
结果:
alice
david
carolina
Python根据缩进来判断代码行与前一个代码行的关系。有缩进代表是for循环体里面的内容,无缩进代表for循环体外的内容,切记!
2 创建数值列表
2.1 使用函数 range()
range(x,y)能生成[x,y)的集合。
for value in range(1,6):
print(value)
结果:
1
2
3
4
5
2.2 使用 range()创建数字列表
要创建数字列表,可使用函数list()将range()的结果直接转换为列表。
numbers = list(range(1,6))
print(numbers)
结果如下:
[1, 2, 3, 4, 5]
使用函数range()时,还可指定步长。例如,下面的代码打印1~10内的偶数:
even_numbers = list(range(2,11,2))
print(even_numbers)
结果:
[2, 4, 6, 8, 10]
2.3 对数字列表执行简单的统计计算
例如,你可以轻松地找出数字列表的最大值、最小值和总和:
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45
2.4 列表解析
列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。
squares = [value**2 for value in range(1,11)]
print(squares)
结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
请注意,这里的for语句末尾没有冒号。
3 使用列表的一部分
3.1 切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
结果:
['charles', 'martina', 'michael']
如果你没有指定第一个索引, Python将自动从列表开头开始:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
由于没有指定起始索引, Python从列表开头开始提取:
['charles', 'martina', 'michael', 'florence']
要让切片终止于列表末尾,也可使用类似的语法。例如,如果要提取从第3个元素到列表末
尾的所有元素,可将起始索引指定为2,并省略终止索引:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])
Python将返回从第3个元素到列表末尾的所有元素:
['michael', 'florence', 'eli']
3.2 遍历切片
如果要遍历列表的部分元素,可在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())
结果:
Here are the first three players on my team:
Charles
Martina
Michael
3.3 复制列表
要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:])。这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表。
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
结果:
My favorite foods are:
['pizza', 'falafel', 'carrot cake']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
注意:
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
结果:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
这里将my_foods赋给friend_foods,而不是将my_foods的副本存储到friend_foods。
这种语法实际上是让Python将新变量friend_foods关联到包含在my_foods中的列表,因此这两个变量都指向同一个列表。
4 元祖
列表是可以修改的,这对处理网站的用户列表或游戏中的角色列表至关重要。然而,有时候你需要创建一系列==不可修改==的元素,元组可以满足这种需求。Python将不能修改的值称为不可变的,而不可变的列表被称为元组。
4.1 定义元组
元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来
访问其元素,就像访问列表元素一样。
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
结果:
200
50
下面来尝试修改元组dimensions中的一个元素,看看结果如何:
dimensions = (200, 50)
dimensions[0] = 250
由于试图修改元组的操作是被禁止的,因此Python指出不能给元组的元素赋值:
Traceback (most recent call last):
File "dimensions.py", line 3, in <module>
dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment
4.2 遍历元组中的所有值
像列表一样,也可以使用for循环来遍历元组中的所有值:
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
就像遍历列表时一样, Python返回元组中所有的元素:
200
50
4.3 修改元组变量
虽然不能修改元组的元素,但可以给存储元组的变量赋值。因此,如果要修改前述矩形的尺
寸,可重新定义整个元组:
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
我们首先定义了一个元组,并将其存储的尺寸打印了出来;接下来,将一个新元组存储到变量dimensions中;然后,打印新的尺寸。这次,Python不会报告任何错误,因为给元组变量赋值是合法的:
Original dimensions:
200
50
Modified dimensions:
400
100