commuting_tools = ['car', 'bicycle', 'bus']
for commuting_tool in commuting_tools:
print('I would like to own a', commuting_tool+'.')
3-4 嘉宾名单
guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
3-5 修改嘉宾名单
guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(guests[-1].title(), 'cannot keep the appointment.')
guests[-1] = 'david'for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
3-6 添加嘉宾
guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(guests[-1].title(), 'cannot keep the appointment.')
guests[-1] = 'david'for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print('I have found a bigger dinner table.')
guests.insert(0, 'eric')
guests.insert(2, 'fred')
guests.append('gay')
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
3-7 缩减名单
guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(guests[-1].title(), 'cannot keep the appointment.')
guests[-1] = 'david'for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print('I have found a bigger dinner table.')
guests.insert(0, 'eric')
guests.insert(2, 'fred')
guests.append('gay')
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print('I can only invite two people to have dinner together.')
while len(guests) > 2:
print('Sorry,', guests.pop().title()+', I feel sorry of being unable to invite you.')
for guest in guests:
print(guest.title()+', you are still in the invitation list.')
del guests[0]
del guests[0]
print(guests)
guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(len(guests), 'guests are invited.')
3-10 尝试使用各个函数
things = [
'mountain',
'river',
'country',
'city',
'language',
'anything I like',
]
print(things)
things.append('append')
print(things)
things.insert(3, 'insert')
print(things)
del things[3]
print(things)
things.pop()
print(things)
things.remove('anything I like')
print(things)
print(sorted(things))
print(things)
things.sort()
print(things)
things.reverse()
print(things)
print(len(things))
3-11 有意引发错误
lists = [i+1for i in range(1, 5)]
# print(lists[4]) # 索引错误
print(lists[3])