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
[b]for item in shoplist:
print(item, end=' ')[/b]

print('\nI also have to buy rice.')
[b]shoplist.append('rice')[/b]
print('My shopping list is now', shoplist)

print('I will sort my list now')
[b]shoplist.sort()[/b]
print('Sorted shopping list is', shoplist)

print('The first item I will buy is', shoplist[0])
[b]olditem = shoplist[0]
del shoplist[0][/b]
print('I bought the', olditem)
newitem = shoplist[0]
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']
>>>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值