1 magicians = ['alice', 'david', 'carolina'] 2 for magician in magicians: 3 print(f"{magician.title()}, that was a great trick!") 4 print(f"I can't wait to see your next trick, {magician.title()}.\n") 5 6 print("Thank you, everyone. That was a great magic show!")
for循环完全是靠缩进来判断代码层次的
1 numbers = list(range(1, 6)) 2 print(numbers)
3 [1, 2, 3, 4, 5]
使用range来创建list range(起始值,最大值,步长) --能得到的是最大值减1
经典处理数字的函数 min() max() sum() 传入list
squares = [value**2 for value in range(1, 11)] print(squares) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
列表解析 快速创建列表
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print(players[0:3]) print("Here are the first three players on my team:") for player in players[:3]: print(player.title()) --------------------------------------- ['charles', 'martina', 'michael'] Here are the first three players on my team: Charles Martina Michael
切片 只使用列表的一部分数据 [开始索引:终止索引]
[:终止索引] 如果没有起始索引就从表头开始
[起始索引:] 如果没有终止索引就到表末
[-3:] 表示从倒数第三到表尾
my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:]
[:] 复制列表 friend_foods = my_foods不可以俩个是同一个对象
元组
元组是不可以修改的
dimensions = (200, 50) for dimension in dimensions: print(dimension) -------------------------------------- 200 50
访问元组是和访问列表是一样的
只有一个元素的元组应该这样显示 xx = (80,) 末尾要加一个逗号
不能修改元组的值 但是可以给元组重新赋值