python学习——修改、添加和删除元素

在上周简要的学习了一下python列表的一些操作,并做了做《python编程——从入门到实践》第三章的一些例题。

3-1、3-2

names=['tony','tom','marry']
print(names[0])
print(names[1])
print(names[2])
print('hello,'+names[0])
print('hello,'+names[1])
print('hello,'+names[2])

3-4、3-5、3-6、3-9

names=['tony','tom','marry']
print('Would you like to eat dinner with me. '+names[0].title()+'?')
print('Would you like to eat dinner with me. '+names[1].title()+'?')
print('Would you like to eat dinner with me. '+names[2].title()+'?')
print(names[0]+" can't come.")
names[0]='jack'
print('Would you like to eat dinner with me. '+names[0].title()+'?')
print('Would you like to eat dinner with me. '+names[1].title()+'?')
print('Would you like to eat dinner with me. '+names[2].title()+'?')
print('I find a bigger table.')
names.insert(0,'evelyn')
names.insert(2,'karin')
names.insert(5,'olof')
print('Would you like to eat dinner with me. '+names[0].title()+'?')
print('Would you like to eat dinner with me. '+names[1].title()+'?')
print('Would you like to eat dinner with me. '+names[2].title()+'?')
print('Would you like to eat dinner with me. '+names[3].title()+'?')
print('Would you like to eat dinner with me. '+names[4].title()+'?')
print('Would you like to eat dinner with me. '+names[5].title()+'?')
print('I can only invite two person.')
name=names.pop()
print('sorry,'+name.title()+".I can't eat dinner with you.")
name=names.pop()
print('sorry,'+name.title()+".I can't eat dinner with you.")
name=names.pop()
print('sorry,'+name.title()+".I can't eat dinner with you.")
name=names.pop()
print('sorry,'+name.title()+".I can't eat dinner with you.")
print(names[0].title()+',I can eat dinner with you.')
print(names[1].title()+',I can eat dinner with you.')
num=len(names)
print('Finally,I invite '+str(num)+' persons.')
del names[1]
del names[0]
print(names)

3-8

travel=['japan','england','france']
print(travel)
print(sorted(travel))
print(travel)
print(sorted(travel,reverse=True))
print(travel)
travel.reverse()
print(travel)
travel.reverse()
print(travel)
travel.sort()
print(travel)
travel.sort(reverse=True)
print(travel)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值