切片:动手试一试

1.切片打印

testlist=['cos','adi','momo','toka','wwc','qqc']
#打印前三个元素
print('The first three items in the list are:',testlist[0:3])
#打印中间三个元素
print('Three items from the middle of the list are:',testlist[1:4])
#打印后三个元素
print('The last three items in the list are:',testlist[-3:])

2.复制副本和循环打印

testlist=['cos','adi','momo','toka','wwc','qqc']
testlistcopy=testlist[:]#直接写列表名代表指向的复制,而不是复制创建副本
testlist.append('123321')
testlistcopy.append(99999)
for a in testlist:
    print(a)
for b in   testlistcopy:
    print(b)
原文地址:https://www.cnblogs.com/xusuns/p/8079179.html