Pythong编程 从入门到实践 练习3-8~3-10

3-8 放眼世界

#存储一些地名并进行一些操作
places = ['pyramids', 'great barrier reef', 'niagara falls', 'louvre', 'venice']
#按原始顺序打印该列表
print(places)
#使用sorted()按字母顺序打印这个列表
print(sorted(places))
#再次打印列表,核实排列顺序未变
print(places)
#使用sorted()按与字母顺序相反的顺序打印这个列表
print(sorted(places,reverse = True))
#再次打印列表,核实排列顺序未变
print(places)
#使用reverse()修改列表的排列顺序
places.reverse()
print(places)
#使用reverse()再次修改列表的排列顺序
places.reverse()
print(places)
#使用sort()修改该列表,使其元素按字母顺序排列
places.sort()
print(places)
#使用sort()修改该列表,使其元素按与字母顺序相反的顺序排列。
places.sort(reverse = True)
print(places)

3-8输出
注意:sorted()是函数,sort()和reverse()是方法

3-9 晚餐嘉宾

#修改嘉宾名单
#添加嘉宾
#缩减名单
guests = ['alice', 'bob', 'carmen', ]
unable_guest = guests.pop(1)
new_guest = 'beer'
guests.insert(1,new_guest)
guests.insert(0,'david')
guests.insert(2,'Eric')
guests.append('ford')
print(guests[0].title() + ',please come and have dinner with me.')
print(guests[1].title() + ',please come and have dinner with me.')
print(guests[2].title() + ',please come and have dinner with me.')
print(unable_guest.title() + ' unable to attend.')
print('I find a bigger table.')
print(guests[3].title() + ',please come and have dinner with me.')
print(guests[4].title() + ',please come and have dinner with me.')
print(guests[5].title() + ',please come and have dinner with me.')
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests[0].title() + ' is still invited.')
print(guests[1].title() + ' is still invited.')
#使用len()打印一条消息,指出邀请了多少位嘉宾
print('I can only invite ' + str(len(guests)) +  ' guests to come and have dinner with me.')
del guests[1]
del guests[0]
print(guests)

3-9输出
之前的习题没有分开保存,倒数第二行打印的是本题的要求。

3-10 尝试使用各个函数

#建一个列表,使用一次本章的每个函数来处理这个列表
cities = ['zurich', 'berlin', 'frankfurt']
#len()函数
print(len(cities))
#sorted()函数
print(sorted(cities))
print(sorted(cities, reverse = True))

3-10输出

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值