数据结构 List
# List examples
shoplist = ['peach','mango','banana']
print('I have',len(shoplist),'items to buy!')
print('These items are:')
for item in shoplist:
print(item)
# After append the list
shoplist.append('rice')
shoplist.append('rose')
print('I have',len(shoplist),'items to buy!')
print('These items are:')
for item in shoplist:
print(item,end=' ')
print('\nSorted done')
# Sort the list
shoplist.sort()
print('After sorted, These items are:',shoplist)
print(shoplist[1])
del(shoplist[1])
print(shoplist)
执行结果
不换行输出,Python 2.x 是加上逗号;Python 3.x 是加上 end=''
I have 3 items to buy!
These items are:
peach
mango
banana
I have 5 items to buy!
These items are:
peach mango banana rice rose
Sorted done
After sorted, These items are: ['banana', 'mango', 'peach', 'rice', 'rose']
mango
['banana', 'peach', 'rice', 'rose']
>>>