笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=7
4.4.1 切片
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print (players[0:3])
['charles', 'martina', 'michael']
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print (players[1:4])
['martina', 'michael', 'florence']
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print (players[:4])
['charles', 'martina', 'michael', 'florence']
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print (players[2:])
['michael', 'florence', 'eli']
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print (players[-3:])
['michael', 'florence', 'eli']
4.4.2 遍历切片
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
4.4.3 复制列表
my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] print ("My favorite foods are:") print (my_foods) print (" My 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 favorite foods are:") print (my_foods) print (" My 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 (" My 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']
4.5.1 定义元组
dimensions = (200, 50) print (dimensions[0]) print (dimensions[1])
200
50
dimensions = (200, 50)
dimensions[0] = 250
dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment
4.5.2 遍历元组中的所有值
dimensions = (200, 50) for dimension in dimensions: print (dimension)
200
50
4.5.3 修改元组变量
dimensions = (200, 50) print ("Origin dimensions:") for dimension in dimensions: print (dimension) dimensions = (400, 100) print (" Modified dimensions") for dimension in dimensions: print (dimension)
Origin dimensions:
200
50
Modified dimensions
400
100
Caesar卢尚宇
2020年3月12日