遍历整个列表
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)//注意这里print 缩进了一行
alice
david
carolina
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
假如第二行不缩进:
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
注意点:
1不要遗忘 “ :”
2注意需要循环的语句进行缩进,不循环的不需要缩进。
创建数值列表
使用range()
for value in range(1,5)://5-1=4个数字,并且从1开始计算
print(value)
1
2
3
4
使用range() 创建数字列表
numbers = list(range(1,6))//list()建个表
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]
1-10的平方:
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
对数字列表执行简单的统计计算:
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)
0>
>> max(digits)
9>
>> sum(digits)
45
列表解析:
squares = [value**2 for value in range(1,11)]
print(squares)
要使用这种语法, 首先指定一个描述性的列表名, 如squares ; 然后, 指定一个左方括号, 并定义一个表达式, 用于生成你要存储到列表中的值。 在这个示例中, 表达式为value2 , 它计算平方值。 接下来, 编写一个for 循环, 用于给表达式提供值, 再加上右方括号。 在这个示例中, for 循环为for value in range(1,11) , 它将值1~10提供给表达式value2 。 请注意, 这里的for 语句末尾没有冒号。
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
使用列表的一部分
players = ['charles', 'martina', 'michael', 'florence', 'eli']
❶ print(players[0:3])
❶ 处的代码打印该列表的一个切片, 其中只包含三名队员。 输出也是一个列表, 其中包含前三名队员:
['charles', 'martina', 'michael']
如果你要提取列表的第2~4个元素, 可将起始索引指定为1 , 并将终止索引指定为4 :
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])//1,2,3//运行到你指定数字之前一位,1代表第二个
['martina', 'michael', 'florence']
如果你没有指定第一个索引, 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']
负数索引返回离列表末尾相应距离的元素, 因此你可以输出列表末尾的任何切片。例如, 如果你要输出名单上的最后三名队员, 可使用切片players[-3:] :
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])
遍历切片:
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
复制列表:
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_foods 中提取一个切片, 从而创建了这个列表的副本, 再将该副本存储到变量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 favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
My favorite foods are:
❹ ['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's favorite foods are:
❺ ['pizza', 'falafel', 'carrot cake', 'ice cream']
直接复制,不创建副本:
下例演示了在不使用切片的情况下复制列表的情况:
my_foods = ['pizza', 'falafel', 'carrot cake']
❶ friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
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', 'cannoli', 'ice cream']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
这里将my_foods 赋给friend_foods , 而不是将my_foods 的副本存储到friend_foods (见❶) 。 这种语法实际上是让Python将新变量friend_foods 关联到包含在my_foods 中的列表, 因此这两个变量都指向同一个列表。 鉴于此, 当我们将’cannoli’ 添加到my_foods 中时, 它也将出现在friend_foods 中; 同样, 虽然’icecream’ 好像只被加入到了friend_foods 中, 但它也将出现在这两个列表中。
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
元组
Python将不能修改的值称为不可变的 , 而不可变的列表被称为元组 。
dimensions = (200, 50)
❷ print(dimensions[0])
print(dimensions[1])
我们首先定义了元组dimensions (见❶) , 为此我们使用了圆括号而不是方括号。 接下来, 我们分别打印该元组的各个元素, 使用的语法与访问列表元素时使用的语法相同(见❷) :
200
50
遍历:
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
200
50