Python 学习笔记 列表 切片 xxxxxxx XXXXXXXXXX
print("=" * 20)
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[1:4])
print(players[:4])
print(players[2:])
print(players[-3:])
for player in players[:3]:
print(player.title())
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)
print("The first three items in the list are:")
print(players[:3])
print("Three items from the middle of the list are:")
print(players[1:4])
print("The last three items in the list are:")
print(players[-3:])
xjs = ['wenxin', 'lvse', 'wuming', 'ysj']
cxj = xjs[:]
xjs.append("kb")
cxj.append("jr")
print(xjs)
print(cxj)
for xj in xjs:
print(xj)
['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']
Charles
Martina
Michael
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
The first three items in the list are:
['charles', 'martina', 'michael']
Three items from the middle of the list are:
['martina', 'michael', 'florence']
The last three items in the list are:
['michael', 'florence', 'eli']
['wenxin', 'lvse', 'wuming', 'ysj', 'kb']
['wenxin', 'lvse', 'wuming', 'ysj', 'jr']
wenxin
lvse
wuming
ysj
kb