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)

你可能感兴趣的:(python学习——修改、添加和删除元素)