WEB开发网
开发学院软件开发Python 简明 Python 教程 -- 第9章 数据结构 阅读

简明 Python 教程 -- 第9章 数据结构

 2007-03-29 11:52:35 来源:WEB开发网   
核心提示: 使用列表例9.1 使用列表#!/usr/bin/python# Filename: using_list.py# This is my shopping listshoplist = ['apple', 'mango', 'carrot',

使用列表

例9.1 使用列表

#!/usr/bin/python
# Filename: using_list.py
# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
  print item,
print ' I also have to buy rice.'
shoplist.append('rice')
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

(源文件:code/using_list.py)

输出

$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']

它如何工作

上一页  1 2 3 4 5 6 7  下一页

Tags:简明 Python 教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接