【高级编程技术】【作业】【第二周】【1】

教材第3章课后练习

3-1 姓名

names = ['alice', 'bob', 'charlie']
print(names[0].title())
print(names[1].title())
print(names[-1].title())

3-2 问候语

names = ['alice', 'bob', 'charlie']
print('Hello,', names[0].title()+'!')
print('Hello,', names[1].title()+'!')
print('Hello,', names[-1].title()+'!')

3-3 自己的列表

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)

3-8 放眼世界

attractions = [
    'Dyrholaey',
    'Nordkapp',
    'Aurora Sky Station',
    'Arnarstapi',
    'OIA',
    'Cliffs of Moher',
    'Mount Fuji',
    'Salar de Uyuni',
    ]
print(attractions)
print(sorted(attractions))
print(attractions)
print(sorted(attractions, reverse=True))
print(attractions)
attractions.reverse()
print(attractions)
attractions.reverse()
print(attractions)
attractions.sort()
print(attractions)
attractions.sort(reverse=True)
print(attractions)

3-9 晚餐嘉宾

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+1 for i in range(1, 5)]
# print(lists[4]) # 索引错误
print(lists[3])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值