依然饭跑跑 发表于 2017-4-26 12:54:41

Python学习笔记(六): list

#!/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:', end=' ') # Notice the comma at end of the line
for item in shoplist:
print(item, end=' ')
print('\nI 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)
olditem = shoplist
del shoplist
print('I bought the', olditem)
newitem = shoplist
print('I bought the', newitem)
print('My shopping list is now', shoplist)

输出:
>>>
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
I bought the banana
My shopping list is now ['banana', 'carrot', 'mango', 'rice']
>>>
页: [1]
查看完整版本: Python学习笔记(六): list