您的位置:首页 > 编程语言 > Python开发

Python List(列表)使用示例

2013-05-15 09:23 537 查看
shoplist = ['apple', 'mango', 'carrot', 'banana'] #定义一个列表

print 'I have', len(shoplist),'items to purchase' #计算列表长度

print 'These items are:',  #遍历列表
for item in shoplist:
print item,

print '\nI also to buy rice'
shoplist.append('rice') #列表当中添加item
print 'My shopping list is now',shoplist

print 'I will sort my list now'
shoplist.sort() #列表进行排序
print 'Sorted shopping list is ',shoplist

print 'The first item I will buy is',shoplist[0]  #访问单个列表元素
olditem = shoplist[0]
del shoplist[0] #删除列表元素
print 'I bought the',olditem
print 'My shopping list is now',shoplist
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: