《Python编程从入门到实践》Chapter 3

# @File    : 3.1列表.py
# @Software: PyCharm

number=[1,2,3,"a ","b" ]
print(number)
print(number[0])
print(number[1])
print(number[4])
print(number[-1].title())
print(number[-2].rstrip())

message="The "+str(number[0])+"st"+" of "+"Alphabet".upper()+" is "+number[3]
print(message)

在这里插入图片描述

# @File    : 3.2修改、添加和删除元素.py
# @Software: PyCharm

guests=['Black','Pink','Orange']
message1="Now I invite "+ guests[0] +"、"+guests[1]+" and "+guests[2]+" to my birthday party on July 7th."
print(message1)
print("For some reason,Blak can't join.")
del guests[0]
guests.insert(0,"Red")
message3="Now I invite "+ guests[0] +"、"+guests[1]+" and "+guests[2]+" to my birthday party on July 7th."
print(message3)
print("By chance,I found a bigger dining-table.")
guests2=['Haha','Xixi','Sea']
guests.insert(0,guests2[0])
guests.insert(2,guests2[1])
guests.append(guests2[2])
message5="Now I invite "+ guests[0] +"、"+guests[1]+"、"+guests[2]+"、"+guests[-3]+"、"+guests[-2]+" and "+guests[-1]+" to my birthday party on July 7th."
print(message5)
print("Unfortunately, I just received the news that the newly purchased large dining tables could not be delivered on time.")
print("So I can only invite two guests to have a birthday dinner together.")
#为了自己看的更清楚,但正式场合不应出现列表
print(guests)
#特别注意:在列表中删除一个元素后,其他元素的位置也会受到影响,所以要注意pop()后面的索引号
first_not_guest=guests.pop(1)
print("I'm terribly sorry,"+first_not_guest+", I can't invite you to my birthday party.")
#如上提醒,后面的索引号都是2
second_not_guest=guests.pop(2)
print("I'm terribly sorry,"+second_not_guest+", I can't invite you to my birthday party.")
third_not_guest=guests.pop(2)
forth_not_guest=guests.pop(2)
print("I'm terribly sorry,"+third_not_guest+" and "+forth_not_guest+", I can't invite you to my birthday party.")
print("I'm terribly sorry,"+third_not_guest+" and "+forth_not_guest+", I can't invite you to my birthday party.")
print("Dear "+guests[0]+" and "+guests[1]+",you are still among the invitees. I hope you will come to my birthday party on time.")
#确定列表长度
print("Finally, I invited "+str(len(guests))+ " people to my birthday party.")
del guests[0]
#再删除第一个元素后,列表中就只剩一个元素,因此下面的索引号仍然是0
del guests[0]
print(guests)

在这里插入图片描述

# @File    : 3.3组织列表.py
# @Software: PyCharm
cities=['Paris', 'Sydney', 'London', 'New York','Singapore']
print(cities)
print(sorted(cities))
print(cities)
print(sorted(cities,reverse=True))
print(cities)
cities.reverse()
print(cities)
cities.reverse()
print(cities)
cities.sort()
print(cities)
cities.sort(reverse=True)
print(cities)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值